Gunicorn + Flask + nginx + wsgi 错误 502 Bad Gateway

Gunicorn + Flask + nginx + wsgi 错误 502 Bad Gateway

我尝试过寻找答案,但大多数人都会遇到此错误uwsginot Gunicorn

nginx错误日志有以下输出:

2019/09/20 17:23:20 [crit] 28847#28847: *2 connect() to unix:/home/ubuntu/app_test/app_test.sock failed (13: Permission denied) while connecting to upstream, client: <client-ip>, server: <server-ip>, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/app_test/app_test.sock:/", host: "<ip-address>"

nginx我在的目录中有以下应用程序的配置文件,sites-available并带有指向 in 的 simlink sites-enabled

server {
    listen 80;
    server_name <server-ip>;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/app_test/app_test.sock;
    }
}

这是服务文件/etc/systemd/system/app_test.service

[Unit]
Description=Gunicorn instance to serve app_test
After=network.target

[Service]
User=ubuntu
Group=ubuntu
WorkingDirectory=/home/ubuntu/app_test
Environment="PATH=/home/ubuntu/app_test/appenv/bin"
ExecStart=/home/ubuntu/app_test/appenv/bin/gunicorn --workers 3 --bind unix:app_test.sock -m 002 wsgi:app

[Install]
WantedBy=multi-user.target

(顺便说一句,为了确定起见,我也尝试了掩码为 的套接字文件007。重新启动服务和 后,日志中的响应仍然相同nginx。)

这是app_test.py文件:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

if __name__ == "__main__":
    app.run(host='0.0.0.0')

最后,这是wsgi.py

from app_test import app

if __name__ == "__main__":
    app.run()

答案1

错误出现在 Gunicorn 服务文件中。由于nginx配置为在www-data组中运行,因此它无权访问该app_test.sock文件。我将组设置更改为www-data,现在效果很好!

相关内容