Question

Django Deployment - Unable to access django app using my ip ?

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"
Subscribe
Share

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.

@hansen @jtittle Thanks for clarifying the issue.

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#

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.