如何解决这个错误 Nginx and uwsgi 502 bad gateway

Posted

技术标签:

【中文标题】如何解决这个错误 Nginx and uwsgi 502 bad gateway【英文标题】:How to solve this error Nginx and uwsgi 502 bad gateway 【发布时间】:2017-02-05 09:36:58 【问题描述】:

我遇到了 502 BadGateway 错误。如何解决?

我抓到了 nginx 访问日志和错误日志

访问日志

120.142.184.164 - - [27/Sep/2016:16:15:48 +0000] "GET / HTTP/1.1" 502 583 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/53.0.2785.116 Safari/537.36"

错误日志

2016/09/27 16:15:48 [crit] 3585#0: *1 connect() to unix:/tmp/PROJECT_NAME.sock failed (13: Permission denied) while connecting to upstream, client: 120.142.184.164, server: 52.78.105.246, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/PROJECT_NAME.sock:", host: "52.78.105.246:8000"

nginx 配置文件

server 

    listen 8000;
    server_name 52.78.105.246;
    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log;

    location = /favicon.ico  access_log off; log_not_found off; 
    location /static/ 
        root /home/ghdalsrn/PROJECT_NAME;
    

    location / 
        include         uwsgi_params;
        uwsgi_pass      unix:/tmp/PROJECT_NAME.sock;
  

uwsgi 配置文件

[uwsgi]
vhost=true
project = PROJECT_NAME
base = /home/ghdalsrn
plugin = python

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

enable-threads = true
master = true
processes = 5

socket = /tmp/%(project).sock
chmod-socket=666


vacuum = true

我用的是 django,nginx 的 configtest 说 OK,not fail。

我什至是root用户,这个错误总是发生。

nginx/1.4.6// Ubuntu 14//

和 pip3 冻结到版本

chardet==2.2.1
colorama==0.2.5
command-not-found==0.3
html5lib==0.999
language-selector==0.1
pbr==1.10.0
pycurl==7.19.3
pygobject==3.12.0
python-apt==0.9.3.5ubuntu2
requests==2.2.1
six==1.10.0
stevedore==1.17.1
uWSGI==2.0.13.1
ufw==0.34-rc-0ubuntu2
unattended-upgrades==0.1
urllib3==1.7.1
virtualenv==15.0.3
virtualenv-clone==0.2.6
virtualenvwrapper==4.7.2
wheel==0.24.0

我该如何解决?

你会帮我吗?

编辑

在 tmp/*.sock 中

srw-rw-r-- 1 root     www-data 0 Sep 27 14:17 /tmp/PROJECT_NAME.sock
srwxrwxr-x 1 ghdalsrn ghdalsrn 0 Sep 27 16:59 /tmp/uwsgi.sock

【问题讨论】:

你能把ls -la /tmp/*.sock的结果发给我们吗? 查看此线程:您可能需要在 uwsgi ini 文件中设置 uidgid 设置:***.com/questions/22071681/… 【参考方案1】:

使用命令运行 uswgi 文件:

uwsgi --ini mysite_uwsgi.ini

【讨论】:

问题中做错了什么?任何相关的文档链接? uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html。 uwsgi 需要启用优雅的重启。如果没有,上面的命令就足够了。

以上是关于如何解决这个错误 Nginx and uwsgi 502 bad gateway的主要内容,如果未能解决你的问题,请参考以下文章

关于ubuntu下 nginx+uwsgi部署flask的坑,有大佬知道为啥吗?

[django] Deploy Django Applications Using uWSGI and Nginx on Ubuntu 14.04

Django / nginx / uwsgi出现502错误

解决websocket使用uWSGI+nginx路由丢失

uwsgi+nginx 出现readv() failed (104: Connection reset by peer)

Django uWSGI NGINX 错误请求 400