簡體   English   中英

Gunicorn + nginx 502 錯誤網關上的 Flask 應用程序

[英]Flask app on Gunicorn + nginx 502 Bad Gateway

我在使用 nxginx 和 gunicorn 部署我的燒瓶應用程序時遇到問題。 當我檢查 nginx 狀態時,它看起來沒問題:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-08-25 20:10:46 CEST; 2s ago
     Docs: man:nginx(8)
  Process: 2847 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 2848 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 2849 (nginx)
    Tasks: 5 (limit: 4915)
   Memory: 8.7M
   CGroup: /system.slice/nginx.service
           ├─2849 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           ├─2850 nginx: worker process
           ├─2851 nginx: worker process
           ├─2852 nginx: worker process
           └─2853 nginx: worker process

sie 25 20:10:46 raspberrypi systemd[1]: Starting A high performance web server and a reverse proxy server...
sie 25 20:10:46 raspberrypi systemd[1]: Started A high performance web server and a reverse proxy server.

Gunicorn 看起來也不錯:

● gunicorn.service - Gunicorn instance to serve peephole
   Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2020-08-25 20:12:11 CEST; 1s ago
 Main PID: 3017 (gunicorn)
    Tasks: 1 (limit: 4915)
   Memory: 39.4M
   CGroup: /system.slice/gunicorn.service
           └─3017 /home/pi/Documents/Peephole/venv/bin/python /home/pi/Documents/Peephole/venv/bin/gunicorn --preload --workers 2 --tim

sie 25 20:12:11 raspberrypi systemd[1]: Started Gunicorn instance to serve peephole.

但是,當我嘗試通過瀏覽器訪問 Flask 應用程序時,我有502 Bad Gateway並且在/var/log/nginx/error.log中看到以下內容(其中 .151 是我嘗試部署應用程序的服務器,並且 . 178是我的桌面):

2020/08/25 18:51:53 [crit] 530#530: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET / HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/", host: "192.168.0.151"
2020/08/25 18:51:54 [crit] 530#530: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/favicon.ico", host: "192.168.0.151", referrer: "http://192.168.0.151/"
2020/08/25 18:52:05 [crit] 530#530: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/favicon.ico", host: "192.168.0.151", referrer: "http://192.168.0.151/"
2020/08/25 19:00:20 [crit] 1299#1299: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET / HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/", host: "192.168.0.151"
2020/08/25 19:00:22 [crit] 1299#1299: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/favicon.ico", host: "192.168.0.151", referrer: "http://192.168.0.151/"
2020/08/25 19:48:32 [crit] 1300#1300: *6 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET / HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/", host: "192.168.0.151"
2020/08/25 19:48:38 [crit] 1300#1300: *6 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/favicon.ico", host: "192.168.0.151", referrer: "http://192.168.0.151/"
2020/08/25 20:12:29 [crit] 2850#2850: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET / HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/", host: "192.168.0.151"
2020/08/25 20:12:29 [crit] 2850#2850: *1 connect() to unix:/home/pi/Peephole/peephole.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/pi/Peephole/peephole.sock:/favicon.ico", host: "192.168.0.151", referrer: "http://192.168.0.151/"

在項目目錄中,我可以看到套接字文件存在,所以很奇怪。

還有什么有趣的,當我通過sudo systemctl stop gunicornsudo systemctl stop nginx and running gunicorn --bind 192.168.0.151 -w 5 wsgi:app sudo systemctl stop nginxgunicorn --bind 192.168.0.151 -w 5 wsgi:app我的應用程序還活着,可以從桌面訪問,但它一直在重新啟動每 20 秒。

獨角獸配置:

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

[Service]
User=pi
Group=www-data
WorkingDirectory=/home/pi/Documents/Peephole
Environment="PATH=/home/pi/Peephole/venv/bin"
ExecStart=/home/pi/Documents/Peephole/venv/bin/gunicorn --preload --workers 2 --timeout 120 --bind unix:peephole.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

nginx配置:

server {
    listen 80;
    server_name 192.168.0.151;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/pi/Peephole/peephole.sock;
    }
}

wsgi.py

from server import create_app

app = create_app()

不知道這是否重要,但 raspberry pi 4b 4gb 是我的服務器。

在主管文件的ExecStart行上,嘗試告訴 gunicorn 綁定到所有接口:

--bind 0.0.0.0:8000

然后在 nginx 配置中,通過 HTTP 而不是套接字傳遞給這個:

proxy_pass http://localhost:8000/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM