Report this

What is the reason for this report?

'502 Bad Gateway' when using gunicorn/nginx/django1.7

Posted on February 25, 2015

i installed django on ubuntu using the one-click install, and am now trying to import an existing django project.

using python manage.py runserver 0:9000 i have no problem, if i go to my site address, i see my project. but if i stop the django ‘runserver’ and run service gunicorn restart, then visit my page, i get a ‘502 Bad Gateway’ error.

i did upgrade my droplet’s django to 1.7 using pip install --upgrade. everything else seems to be working fine

on a related point… i am new to servers, and still unclear of what nginx, gunicorn, and wsgi are, and what the flow of information is between them, so if someone could briefly explain that, or point me to a good tutorial, much appreciated.

my nginx error log says the following:

2015/02/25 16:53:16 [error] 1052#0: *191 connect() failed (111: Connection refused) while connecting to upstream, client: 76.98.74.102, server: _, request: "GET /home/ HTTP/1.1", upstream: "http://127.0.0.1:9000/home/", host: "104.236.29.98"

my django project is in /home/django/myproject/

/etc/init/gunicorn.conf:

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

respawn 

setuid django
setgid django
chdir /home/django

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

/etc/nginx/sites-available/myproject:

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;

    server_name localhost;

    keepalive_timeout 5;

    location /media  {
        alias /home/django/myproject/media;
    }

    location /static {
        alias /home/django/myproject/mysite/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;
    }

}

mproject/mysite/wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

I had that issue as well. But my problem was that I did not have gevent installed. The gunicorn log showed an error that said: gevent is required to run this worker or something similar. So, installing gevent: pip install gevent, fixed it for me.

I have changed the following line

WorkingDirectory=/home/mushegh/artkaren

to

WorkingDirectory=/home/mushegh/artkaren/artkaren

et voila, it works!!!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.