Gunicorn + Flask + nginx + wsgi 오류 502 잘못된 게이트웨이

Gunicorn + Flask + nginx + wsgi 오류 502 잘못된 게이트웨이

나는 답변을 찾으려고 노력했지만 대부분의 사람들은 이 오류를 얻지 uwsgi못했습니다 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>"

nginxsites-available다음을 가리키는 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이제 잘 작동합니다!

관련 정보