Python Django+Nginx+uwsgi 502 Bad Gateway

2019-08-16 15:52发布

问题:

Centos7,when I connect to my website, shows 502 Bad Gateway,
I test my website with command
uwsgi --ini
systemctl start nginx
And I cant figure out what's happened,please help me!

here's nginx.conf

upstream django {
  server 127.0.0.1:8000;
}

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  example.com;
    charset      utf-8;

    include /etc/nginx/default.d/*.conf;

    location / {
      include uwsgi_params;
      uwsgi_pass django;
    }

    location /static/ {
      alias /usr/local/etc/dmp/static/;
    }
}

and uwsgi setting

[uwsgi]

chdir = /usr/local/etc/dmp

module = DMP_python.wsgi

plugins = python3

socket = :8000

chmod-socket = 666

master = true

processes = 2

vacuum = true

回答1:

You're using the wrong setting to tell uwsgi to use an HTTP port. You need http-socket rather than socket.



回答2:

There can be multiple reasons for which an upstream might return invalid or even do not return any response

  • Verify if upstream uwsgi is actually running locally in the centos instance and can handle incoming requests

    1. for this to verify run it as http-socket = :8000 in uwsgi.ini and then run uwsgi --ini uwsgi.ini

    2. if uwsgi running fine on localhost, then change config back to socket = :8000

  • On centos 7.x SELinux package is enabled and runs in enforcing mode. So it won't allow nginx to write/connect to a socket.

    1. verify if SELinux has the policy for nginx to write to sockets
    2. check if read/connect/write to socket is allowed grep nginx /var/log/audit/audit.log | audit2allow -m nginx
    3. do so by grep nginx /var/log/audit/audit.log | audit2allow -M nginx
    4. and finally semodule -i nginx.pp
    5. permission to connect to socket issue should be resolved by now
    6. verify if nginx can do network connection with upstream. Check nginx error.log or getsebool -a | grep httpd
    7. to allow it run setsebool httpd_can_network_connect on -P