user673869
user673869

Reputation: 231

gunicorn, nginx, and using port 80 for running a django web application

I have django, nginx, and gunicorn installed on a web server.

Nginx listens on port 80 Gunicorn runs django project on port 8000

This works fine. If I go to www.mysite.com:8000/myapp/ the django application comes up OK. But what if I want users to go to www.mysite.com/myapp/ to view the django application? I don't think getting rid of Nginx is the answer, and I'm hoping I missed some configuration tweak i can apply to make this work.

Any advice is appreciated.

Upvotes: 3

Views: 6351

Answers (2)

Danial Tz
Danial Tz

Reputation: 1984

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

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599600

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.

Upvotes: 2

Related Questions