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

February 25, 2015 10k views

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:, 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/gunicorn.py \


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()
  • Are there any additional error messages in /var/log/upstart/gunicorn.log ?

  • 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?

  • 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.

  • 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.

    by Justin Ellingwood
    In this guide, we will be setting up a simple Python application using the Flask micro-framework on Ubuntu 14.04. The bulk of this article will be about how to set up the Gunicorn application server to launch the application and Nginx to act as a front end reverse...
  • Did anyone solve this problem?

  • @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

3 Answers

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




et voila, it works!!!

Have another answer? Share your knowledge.