1

I'm trying to deploy a django application to AWS EC2 instance using Gunicorn & nginx. I followed this tutorial because it worked for me last time, but now it's not.

I always get 404 response, even on static files.

Supervisor script:

[program:django]
command = /home/ubuntu/bin/gunicorn_start
user = ubuntu
stdout_logfile = /home/ubuntu/logs/gunicorn_supervisor.log
redirect_stderr = true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8

gunicorn_start:

#!/bin/bash

NAME="django"
DJANGODIR=/home/ubuntu/django_project
SOCKFILE=/home/ubuntu/run/gunicorn.sock
USER=ubuntu
NUM_WORKERS=3
DJANGO_SETTINGS_MODULE=django_project.settings
DJANGO_WSGI_MODULE=django_project.wsgi

echo "Starting $NAME as `whoami`"

cd $DJANGODIR
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-

Nginx Configuration:

upstream django_server {
    server unix:/home/ubuntu/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name _;

    client_max_body_size 4G;

    access_log /home/ubuntu/logs/nginx-access.log;
    error_log /home/ubuntu/logs/nginx-error.log;

    location /static/ {
        alias /home/ubuntu/django_project/static_root/;
    }
    location /media/ {
        alias /home/ubuntu/django_project/media/;
    }
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxt_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://applify_server;
    }

    error_page 500 502 503 v504 /500.html;
    location = /500.html {
        root /home/ubuntu/django_project/static_root/;
    }
}

The nginx logs are empty, and the supervisor log don't show any errors. The same applies to nginx access logs and error logs! Nginx is being run as root. A DNS record has not yet being created. The site is being accessed with it's IP.

e4c5
  • 52,766
  • 11
  • 101
  • 134
Ofersto
  • 107
  • 2
  • 9

1 Answers1

0

It appears first that you have not deleted the nginx default site that is created when first installed. Your request to the server using the elastic IP appears to be handled by that default site. This is indicated by the fact that your access log file is empty. Solution: delete records for other sites and leave only this configuration file in sites_enabled.

Other issues: You are running nginx as root. this is not advisable. Please change to an unprivleged user.

Your static content is in /home/ubuntu/django_project/static_root/; this folder will not be accessible to nginx because home folders are usually accessible to the owner. You may need to change the /home/ubuntu/ folder permissions. However it's better still to move the files to a different location.

e4c5
  • 52,766
  • 11
  • 101
  • 134