502 Bad Gateway Ngnix & Gunicorn

January 20, 2014 56.4k views
Hey guys, I followed these: https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn intrstuctions to get my Django project up and running on my Droplet. Problem is I'm getting a 502 Bad Gateway Error for some strange reason. Has anyone stumbled upon the same issue? All help is appreciated. Thanks!
13 Answers
You can pass --log-file to gunicorn when you launch it to specify where the log file gets stored. Any output there would be helpful in figuring this out.

In your settings.py file have you set DEBUG to False? If so, remember that you need to add your IP to ALLOWED_HOSTS.
Check Gunicorn's error logs, what do you see?
Hi Kamal,

I have the same issue and the log says:

2014/03/06 21:42:23 [error] 2472#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "", host: ""

I don't have a domain. I use the droplet's ip.

Can you help me? Thanks in advance.
@jbkunst: That means nginx is trying to proxy traffic to port 8001 but there's nothing listening on that port. Is Gunicorn running?

Anything on this error? It's cropping up all over.
Also having the same problem, Ismail. Any luck figuring it out?
@kpennel: Is Gunicorn running?
sudo netstat -plutn | grep -i unicorn

sudo ps wwaux | grep grep -i unicorn
  • When I run the first line I get nothing. When I run the second, I get.

    root      3493  0.0  0.1  11740   948 pts/0    S+   14:19   0:00 grep --color=auto -i unicorn
From the tutorial you mentioned and after configuring nginx, you should change your Gunicorn configuration bind variable to bind='' (not your domain or ip address) as it is nginx the one who handle the requests.
I was having the same issue after following these https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn instructions and I found my way to this page.

After trying tomas.caslo90's suggestion I ended up with a Bad Request (400) instead of a 502 Bad Gateway.

should gunicorn bind to or to mydomain.com:8001??

any suggestions for further troubleshooting on this issue. i have followed the instructions in the above article very carefully. gunicorn is definitely running.

I don't know how to access gunicorn's error logs and I'm guessing that might help me greatly.

Thanks very much
by Stephen Tippets
This tutorial runs through both installing and configuring Django with Postgres, Nginx, and Gunicorn on your VPS.
I have the same issue as brentlunghino and I would love some additional help.

when i open the IP address in browser i got this: The requested URL / was not found on this server.
however, i can got to admin page but the css is not rendered.


2015/01/26 14:53:35 [error] 5935#0: *2 open() “/bootstrap/js/bootstrap.min.js” failed (2: No such file or directory), client:, server:, request: “GET /static/bootstrap/js/bootstrap.min.js HTTP/1.1”, host: “”, referrer: “
2015/01/26 14:55:44 [error] 5935#0: *8 open() “/css/hamburger.css” failed (2: No such file or directory), client:, server:, request: “GET /static/css/hamburger.css HTTP/1.1”, host: “”

this is how i run my server: gunicorn -b joyceful.wsgi:application

this is my nginx config file:

server {
server_name myIPaddress;

access_log off;

location /static/ {
    alias /home/yshang/joyceful-venv/joycful/static/;

location / {
    proxy_pass http://myIPaddress:8001;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;


please help. thanks

I had the same and just had to restart gunicorn with service supervisor restart

Hope that helps.

I had a similar problem (502) after creating a new folder as an alternative to the out-of-the-box django_project.

I saw in /var/log/upstart/gunicorn.log that it was getting a permission error on /home/django.

I fixed it by …

$ cd /home
$ chown -R django:django django
Have another answer? Share your knowledge.