ckayorke
By:
ckayorke

Help Setting up Djanjo, supervisor, nginx

January 30, 2016 1.1k views
Django Python Frameworks Nginx

I have a requirement to update an existing django project. but how the project was set up is a bit complex. The settings.py is in its on folder called toolbox instead of the main project folder. The toolbox folder is inside the main project folder called mrv which is also in a virtualevn called mrv-env. Below some important aspect of the settings.py:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ROOT_URLCONF = 'toolbox.urls'
WSGI_APPLICATION = 'toolbox.wsgi.application'
TATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
STATIC_URL = '/static/'
code

The Django aspect runs OK. The problem is setting up the gunicorn, supervisor, and nginx. Below is the gunicorn_start file located in the virtualenv mrv-env bin folder

#!/bin/bash

NAME="toolbox"
DJANGODIR=/home/user/mrv-env/mrv
SOCKFILE=/home/user/mrv-env/run/gunicorn.sock
GROUP=sudo
USER=user
NUM_WORKERS=2
DJANGO_SETTINGS_MODULE=toolbox.settings
DJANGO_WSGI_MODULE=toolbox.wsgi

cd $DJANGODIR
source ../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

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

# Start your Django unicorn
exec /home/user/mrv-env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER
  --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-

Below is the supervisor portion

[program:usergunicorn]
command = /home/user/mrv-env/bin/gunicorn_start
user=user
autorestart = true
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8
stdout_file = /home/user/mrv-env/log/gunicorn.log
stderr_file = /home/user/mrv-env/log/gunicorn.error.log
directory = /home/user/mrv-env/mrv/

Lastly below the gninx portion

upstream helena_server {
    server unix:/home/user/mrv-env/run/gunicorn.sock fail_timeout=0;
}

server {
    listen   80;
    server_name mrvuser.com;
    # no security problem here, since / is alway passed to upstream
    root /home/user/mrv-env/mrv/;
    # serve directly - analogous for static/staticfiles

    location /media/ {
       alias /home/user/mrv-env/mrv/media/;
        expires 1y;
    }

    location /static/ {
       autoindex on;
        expires 1y;
        alias /home/user/mrv-env/mrv/static/;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;

}

The problem is when I go to the url, it is ngixn welcome information that shows instead of my project. Can some tell me what is going wrong. I have spend more than two day on this step.

1 Answer

The first thing that jumps out at me reading this over is that while you've defined an upstream server pointing to a socket file, the proxy pass is actually pointing to localhost port 8000. It should look like:

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://helena_server;
    }

Though, it seems that there must be something else going on here as normally I'd expect this configuration to display a 502 Bad Gateway error rather than the Nginx default page. Is it possible you have another Nginx configuration in /etc/nginx/sites-enabled/ that is taking precedence?

Have another answer? Share your knowledge.