Nginx – UWSGI Bad Gateway – Connection refused while connecting to upstream

502502-errordjangonginxuwsgi

Trying to get a basic Django app running on nginx using UWSGI. I keep getting a 502 error with the error in the subject line. I am doing all of this as root, which I know is bad practice, but I am just practicing. My config file is as follows (it's included in the nginx.conf file):

server {
listen 80;
server_name 104.131.133.149;

location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/root/headers;
}

location / {
    include         uwsgi_params;
    uwsgi_pass      127.0.0.1:8080;
}
}

And my uwsgi file is:

[uwsgi]
project = headers
base = /root

chdir = %(base)/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application

master = true
processes = 5

socket = 127.0.0.1:8080
chmod-socket = 666
vacuum = true

As far as I can tell I am passing all requests on port 80 (from nginx.conf) upstream to localhost, which is running on my VH, where uwsgi is listening on port 8080. I've tried this with a variety of permissions, including 777. If anyone can point out what I'm doing wrong please let me know.

Best Answer

Ok so the issue was not starting up uwsgi with --socket 127.0.0.1:8080 option set! That was in my upstart script which now looks like:

start on runlevel [2345]
stop on runlevel [!2345]

setuid root
setgid root

exec /usr/local/bin/uwsgi --emperor /etc/uwsgi/sites --socket 127.0.0.1:8080