[Django]-Gunicorn, nginx, and using port 80 for running a django web application

2👍

No, getting rid of nginx is definitely not the answer. The answer is to follow the very nice documentation to configure nginx as a reverse proxy to gunicorn.

3👍

You can use the following configuration, so you can access your website normally on port 80:

this is your nginx configuration file, sudo vim /etc/nginx/sites-available/django

upstream app_server {
        server 127.0.0.1:9000 fail_timeout=0;
    }
    server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        root /usr/share/nginx/html;
        index index.html index.htm;
        client_max_body_size 250M;
        server_name _;
        keepalive_timeout 15;

# Your Django project's media files - amend as required
        location /media  {
            alias /home/xxx/yourdjangoproject/media;
        }
        # your Django project's static files - amend as required
        location /static {
            alias /home/xxx/yourdjangoproject/static;
        }
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app_server;
        }
    }

and configure gunicorn as

description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid yourdjangousernameonlinux
setgid yourdjangousernameonlinux
chdir /home/xxx/yourdjangoproject

exec gunicorn \
    --name=yourdjangoproject \
    --pythonpath=yourdjangoproject \
    --bind=0.0.0.0:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    yourdjangoproject.wsgi:application

Leave a comment