evvan
By:
evvan

502 Bad Gateway Django Nginx Gunicorn

January 4, 2017 1.5k views
Python Frameworks Ubuntu

I'm trying to run a django website on a server through nginx and gunicorn. I followed these instructions to get setup but I'm receiving a bad gateway error when I google my ip. If anyone can help thanks so much! Best, Evvan

https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

Also when I do status of nginx and gunicorn I get:

derians@myCodingBlog:~$ sudo systemctl status nginx

[sudo] password for derians:

nginx.service - A high performance web server and a reverse proxy server

Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)

Active: active (running) since Tue 2017-01-03 23:50:52 UTC; 15h ago

Main PID: 28998 (nginx)

Tasks: 2

Memory: 2.1M

CPU: 163ms

CGroup: /system.slice/nginx.service
|-28998 nginx: master process /usr/sbin/nginx -g daemon on; master_process on

`-28999 nginx: worker process

Jan 03 23:50:52 myCodingBlog systemd[1]: Stopped A high performance web server and a reverse proxy server.

Jan 03 23:50:52 myCodingBlog systemd[1]: nginx.service: Unit entered failed state.

Jan 03 23:50:52 myCodingBlog systemd[1]: nginx.service: Failed with result 'timeout'.

Jan 03 23:50:52 myCodingBlog systemd[1]: Starting A high performance web server and a reverse proxy server...

Jan 03 23:50:52 myCodingBlog systemd[1]: nginx.service: Failed to read PID from file /run/nginx.pid: Invalid argument

Jan 03 23:50:52 myCodingBlog systemd[1]: Started A high performance web server and a reverse proxy server.

derians@myCodingBlog:~$ sudo systemctl status gunicorn

gunicorn.service - gunicorn daemon

Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)

Active: failed (Result: exit-code) since Wed 2017-01-04 00:50:22 UTC; 14h ago

Main PID: 29271 (code=exited, status=203/EXEC)
Jan 04 00:50:22 myCodingBlog systemd[1]: Started gunicorn daemon.

Jan 04 00:50:22 myCodingBlog systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC

Jan 04 00:50:22 myCodingBlog systemd[1]: gunicorn.service: Unit entered failed state.

Jan 04 00:50:22 myCodingBlog systemd[1]: gunicorn.service: Failed with result 'exit-code'.

1 comment
3 Answers

Can you share your nginx configuration here?

The most likely reason for a 502 error is if either

  1. Your app is not running so nginx cannot connect to it.
  2. Your app is listening on a different socket or port than what is configured in nginx.

You can attempt to reach your service directly on your droplet by using curl or wget to verify it is listening and check your logs for more information.

The more detail you can share here the better we'll be able to assist you in getting to the bottom of it.

I figured out it was because I was runnin it with Python 2 and it needs Python 3 ... I wiped the server and reinstalled everything but ran all my apps in Python 3 instead of 2 and it worked

I have the same problems, here is my vps description
8 core processor
16GB RAM
160 GB SSD
6TB Tranfers

i am using, uwsgi(2.0.14), nginx(1.10), mysql(5.6 )and ubuntu 16.04 LTS
django 1.10.x and python 2.7.x

but the different is my deploy i using is uwsgi and mysql
a share my config

---- uwsgi script in /etc/uwsgi/app-availabre/website.ini -----
http://pastebin.com/vQW47WGb

---- nginx conf in /etc/nginx/nginx.conf ------
http://pastebin.com/5BsWWaps

----- nginx vhost for my website mydomain --------
http://pastebin.com/bMPvmrwR

Any help, I appreciate it

Have another answer? Share your knowledge.