nginx uwsgi 502 错误网关

nginx uwsgi 502 Bad Gateway

我的 nginx 和 uwsgi 配置非常适合前一个或两个请求。然后 nginx 显示 502 Bad Gateway 。当我重新启动 uwsgi 服务时,同样的事情再次发生。我正在使用 Ubuntu 16.04。这是我所有的 conf 和错误日志:

nginx conf:

upstream book {
    server unix:///tmp/book.sock;
}

server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
   listen 443 ssl http2;
   server_name example.com;
    ......
   add_header X-Frame-Options DENY;
   add_header X-Content-Type-Options nosniff;

   charset utf-8;

    client_max_body_size 100M;

    access_log  /var/log/nginx/example.com_access.log;
    error_log   /var/log/nginx/example.com_error.log;

    location /media {
        alias /home/prism/prod/example.com/media;
    }

    location /static {
        alias /home/prism/prod/example.com/static;
    }

    location / {
        uwsgi_pass book;
        include     /etc/nginx/uwsgi_params;
    }

}

/var/log/nginx/example.com_error.log

2016/05/25 17:44:26 [error] 5230#5230: *214 connect() to unix:///tmp/book.sock failed (111: Connection refused) while connecting to upstream, client: 27.*.*.*, server: example.com, request: "GET / HTTP/2.0", upstream: "uwsgi://unix:///tmp/book.sock:", host: "example.com"

hiren.ini

[uwsgi]
chdir=/home/prism/prod/example.com
home = /home/prism/prod/example.com/.env
module=hiren.wsgi
master=True
process = 5
pidfile=/tmp/book.pid
socket= /tmp/book.sock
vacuum=True
max-requests=5000
daemonize=/home/prism/prod/example.com/hiren.log
uid = www-data
gid = www-data
die-on-term = true

和服务文件:

[Unit]
Description=uWSGI instance to serve example.com

[Service]
User=prism
ExecStart=/bin/bash -c 'cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'

[Install]
WantedBy=multi-user.target

通过调整 systemd 脚本解决了问题

[Unit]
Description=uWSGI instance to serve example.com

[Service]
ExecStart=/bin/bash -c 'su prism; cd /home/prism/prod/example.com; source .env/bin/activate; uwsgi --ini hiren.ini'

[Install]
WantedBy=multi-user.target

如果您能够 运行 uwsgi 和您的虚拟环境中的 .ini 文件,并且 nginx 已充分启动,则问题出在 systemd 文件上。