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

  • Posted 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 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:, server: _, request: "GET /home/ HTTP/1.1", upstream: "", host: ""

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


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


setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=open_elections \
    --pythonpath=open_elections \
    --bind= \
    --config /etc/gunicorn.d/ \


upstream app_server {
    server 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;



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

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

@pav, yes i ultimately resolved it, but it’s been so long now, i don’t remember how. and i stopped working on the site soon thereafter, so my droplet is now gone. all i remember was uninstalling and re-installing stuff in various combinations of order and settings. but in the end, it may have been something stupid like a port number not matching. i don’t remember. sorry i cant be more help

Did anyone solve this problem?

I’m having the same problem, don’t actually see any errors, just can’t get my django project to show up. Really strange since I followed this flask tutorial and it worked fine! So how come flask knows what to do and django is choking?

fwiw, I’m noticing that nginx doesn’t seem to be finding my static files…? that’s probably not helping.

I’m having the same problem, and my gunicorn.log contains this repeatedly:

[2015-05-03 19:07:43 +0000] [1819] [INFO] Starting gunicorn 19.3.0
[2015-05-03 19:07:43 +0000] [1819] [ERROR] Retrying in 1 second.
[2015-05-03 19:07:44 +0000] [1819] [ERROR] Retrying in 1 second.
[2015-05-03 19:07:45 +0000] [1819] [ERROR] Retrying in 1 second.
[2015-05-03 19:07:46 +0000] [1819] [ERROR] Retrying in 1 second.
[2015-05-03 19:07:47 +0000] [1819] [ERROR] Retrying in 1 second.
[2015-05-03 19:07:48 +0000] [1819] [ERROR] Can't connect to /home/imgmix/imgmix_core/imgmix_core.sock
[2015-05-03 19:07:48 +0000] [1823] [INFO] Starting gunicorn 19.3.0
[2015-05-03 19:07:48 +0000] [1823] [ERROR] Retrying in 1 second.

I have checked gunicorn.conf, and it is set to the same user and group as my project files.

Pretty frustrated with this error message, especially after seeing the same question unanswered time and time again. Can anyone FINALLY shed some light into this?

Are there any additional error messages in /var/log/upstart/gunicorn.log ?

Submit an 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.

I have changed the following line




et voila, it works!!!

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.

This comment has been deleted