【问题标题】:Python Django+Nginx+uwsgi 502 Bad GatewayPython Django+Nginx+uwsgi 502 网关错误
【发布时间】:2019-01-01 05:21:14
【问题描述】:

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

【问题讨论】:

  • 什么是nginx错误日志?
  • 你启动过 django 吗?
  • connect() to 127.0.0.1:8000 failed (13: Permission denied) 同时连接到上游,但我已经修改了 nginx.conf -> 用户 root;但仍然是徒劳的

标签: python django nginx centos7 uwsgi


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

      1. 为此验证在 uwsgi.ini 中将其作为 http-socket = :8000 运行,然后运行 ​​uwsgi --ini uwsgi.ini

      2. 如果 uwsgi 在 localhost 上运行良好,则将配置改回 socket = :8000

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

      1. 验证 SELinux 是否有 nginx 写入套接字的策略
      2. 检查是否允许读取/连接/写入套接字grep nginx /var/log/audit/audit.log | audit2allow -m nginx
      3. 这样做grep nginx /var/log/audit/audit.log | audit2allow -M nginx
      4. 最后是semodule -i nginx.pp
      5. 现在应该解决连接到套接字问题的权限
      6. 验证 nginx 是否可以与上游进行网络连接。检查 nginx error.log 或getsebool -a | grep httpd
      7. 允许它运行setsebool httpd_can_network_connect on -P

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2020-06-29
      • 2017-06-23
      • 1970-01-01
      • 2016-02-16
      • 2012-07-10
      • 2020-09-29
      相关资源
      最近更新 更多