By ozwiz
I have followed https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn tutorial to deploy django app. At the end of it, when i tried accessing I got “502 Bad Gateway”.
Here are nginx error logs from error.logs file.
2017/03/01 06:40:13 [error] 26234#26234: *16 connect() failed (111: Connection refused) while connecting to upstream, client: 85.203.18.254, server: 174.138.68.184, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "174.138.68.184"
2017/03/01 06:41:00 [error] 26301#26301: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 85.203.18.254, server: 174.138.68.184, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "174.138.68.184"
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!
It’s because Gunicorn is not running, so when Nginx cannot connect to the upstream server it gives the error 502. Did you remember to start Gunicorn? It’s in the lower half of step 9.
Yes gunicorn is running.
(myenv) root@ubuntu-512mb-nyc3-01:/opt# sudo ps wwaux | grep -i unicorn
root 27360 0.1 4.5 61988 22848 pts/2 S+ 08:03 0:00 /opt/myenv/bin/python3 /opt/myenv/bin/gunicorn --bind 174.138.68.184:8001 django_project.wsgi
root 27363 0.0 7.2 158764 36412 pts/2 S+ 08:03 0:00 /opt/myenv/bin/python3 /opt/myenv/bin/gunicorn --bind 174.138.68.184:8001 django_project.wsgi
root 27493 0.0 0.2 12944 1084 pts/3 S+ 08:08 0:00 grep --color=auto -i unicorn
(myenv) root@ubuntu-512mb-nyc3-01:/opt#
Infact i can access django app at http://174.138.68.184:8001/ even when i turn off nginx. That means i guess gunicorn is working as expected.
I also tried to view error logs of gunicorn by specifying --log-file as below:
gunicorn --bind 174.138.68.184:8001 --log-file g.log django_project.wsgi
Here is the content of the log file.
(myenv) root@ubuntu-512mb-nyc3-01:/opt/django_project# cat g.log
[2017-03-01 08:14:33 +0000] [27546] [INFO] Starting gunicorn 19.6.0
[2017-03-01 08:14:33 +0000] [27546] [INFO] Listening at: http://174.138.68.184:8001 (27546)
[2017-03-01 08:14:33 +0000] [27546] [INFO] Using worker: sync
[2017-03-01 08:14:33 +0000] [27549] [INFO] Booting worker with pid: 27549
(myenv) root@ubuntu-512mb-nyc3-01:/opt/django_project#
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.