Nginx 502 bad gateway (Python app)

October 5, 2016 516 views
Nginx Python Ubuntu 16.04

I have been following this tutorial to get a simple Python app working with NGINX and uwsgi on an Ubuntu server

I managed to get the 'hello' part to display on my ip at port 8080 as requested but then at the end when I am supposed to be able to see that text on the ip and not just port 8080 I get a 502 bad gateway error.

I have been over the code many times and for hrs as well as reading other threads here and on places like Stackoverflow and nothing seems to work. Some people have suggested changing the chmod-socket from 664 to 666 or 600 and neither of those work.

I have been reading this similar thread too

it seems this person has a slightly different nginx con file setup but when Ive tried adding the things such as this to my con file

$ exec uwsgi --ini app.ini

it still doesn't work, however if I run that command in my terminal I can get the 'Hello there!' text to appear on all ports but according to the tutorial Im not supposed to have to run that to get it to work. When I do run that it seems to spawn multiple workers also (not sure if its supposed to do that) and this is the output

*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x22a37d0 pid: 17920 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 17920)
spawned uWSGI worker 1 (pid: 17940, cores: 1)
spawned uWSGI worker 2 (pid: 17941, cores: 1)
spawned uWSGI worker 3 (pid: 17942, cores: 1)
spawned uWSGI worker 4 (pid: 17943, cores: 1)
spawned uWSGI worker 5 (pid: 17944, cores: 1)

this is my nginx log output and it looks like there is a problem with the sock file

[crit] 17492#0: *1 connect() to unix:///home/david/myapp/myapp.sock failed (2: No such file or directory) while connecting to upstream, client:, server:, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:///home/david/myapp/myapp.sock:", host: "128.199.97..37"

but I dont know how to fix this, all that is mentioned in the tutorial is that the mayor.ini file should have this line in

$ socket = myapp.sock

which my .ini file has, so what is going on here? Is the sock file not being created or something?

I should add that nginx, uwsgi and my app are all running

1 Answer
ryanpq MOD October 6, 2016
Accepted Answer

I think that this may be where you ran into trouble:

Create an Upstart File to Manage the App

The tutorial you linked to is specific to Ubuntu 14.04 but your question is tagged 16.04. Between these releases, Ubuntu switched their init system to systemd and no longer provides proper support for upstart jobs. I would recommend either following that tutorial on Ubuntu 14.04 which is still supported or digging into systemd to create a service file manually.

Have another answer? Share your knowledge.