Python Django + Nginx + uwsgi 502错误网关

时间:2018-07-24 14:24:05

标签: python django nginx centos7 uwsgi

当我连接到我的网站时,Centos7显示502 Bad Gateway,
我用命令测试我的网站
uwsgi --ini
systemctl启动nginx
而且我不知道发生了什么事,请帮助我!

这是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/;
    }
}

和uwsgi设置

[uwsgi]

chdir = /usr/local/etc/dmp

module = DMP_python.wsgi

plugins = python3

socket = :8000

chmod-socket = 666

master = true

processes = 2

vacuum = true

2 个答案:

答案 0 :(得分:0)

您使用了错误的设置来告诉uwsgi使用HTTP端口。您需要http-socket而不是socket

答案 1 :(得分:0)

上游有多个原因可能返回无效甚至不返回任何响应

  • 验证上游uwsgi是否实际上在centos实例中本地运行并且可以处理传入请求

    1. 为此进行验证,以在uwsgi.ini中以 Gson gson = new GsonBuilder() .setDateFormat(Constants.TimeStamp + System.currentTimeMillis() + Constants.Slash) .create(); Gson gson = new GsonBuilder() .registerTypeAdapter(Id.class, new IdTypeAdapter()) .enableComplexMapKeySerialization() .serializeNulls() .setDateFormat(Constants.TimeStamp + System.currentTimeMillis() + Constants.Slash) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .setVersion(1.0) .create(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(API.BASE_URL) .addConverterFactory(GsonConverterFactory.create(gson)) .build(); 的身份运行它,然后运行http-socket = :8000

    2. 如果uwsgi在本地主机上运行良好,则将配置更改回uwsgi --ini uwsgi.ini

  • 在centos 7.x上,SELinux软件包已启用并以强制模式运行。因此,它不允许nginx写入/连接到套接字。

    1. 验证SELinux是否具有nginx写入套接字的策略
    2. 检查是否允许对套接字进行读/连接/写操作socket = :8000
    3. 通过grep nginx /var/log/audit/audit.log | audit2allow -m nginx
    4. 最后是grep nginx /var/log/audit/audit.log | audit2allow -M nginx
    5. 现在应该解决允许连接套接字的问题
    6. 验证nginx是否可以与上游进行网络连接。检查nginx error.log或semodule -i nginx.pp
    7. 允许它运行getsebool -a | grep httpd
相关问题