【问题标题】:Flask app on Gunicorn + nginx 502 Bad GatewayGunicorn + nginx 502 Bad Gateway 上的 Flask 应用程序
【发布时间】:2020-12-14 12:15:17
【问题描述】:

我在使用 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.

但是,当我尝试通过浏览器访问烧瓶应用程序时,我有 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/"

在项目目录中我可以看到socket文件存在,所以很奇怪。

还有什么有趣的,当我通过 sudo systemctl stop gunicorn 和 sudo systemctl stop nginx 停止 nginx 和 gunicorn 并运行 gunicorn --bind 192.168.0.151 -w 5 wsgi:app 时,我的应用程序是活动的并且可以从桌面访问,但它会每 20 秒重新启动一次。

Gunicorn 配置:

[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()

不知道这是否重要,但树莓派 4b 4gb 是我的服务器。

【问题讨论】:

  • 你的 gunicorn 配置在哪里?你从哪里得到套接字路径http://unix:/home/pi/Peephole/peephole.sock?
  • 你检查/home/pi/Peephole/peephole.sock文件的权限了吗?
  • @jordanm gunicorn 配置路径:/etc/systemd/system/gunicorn.service/。套接字文件已按照本教程创建:digitalocean.com/community/tutorials/…@IvanVelichko 文件权限:srwxrwx--- 1 pi www-data
  • 我明白了,应该是/home/pi/Documents/Peephole/peephole.sock,而不是/home/pi/Peephole/peephole.sock。
  • 哇,你是对的......如此明显...... :) 谢谢!

标签: python nginx flask raspberry-pi gunicorn


【解决方案1】:

在您的主管文件中的 ExecStart 行,尝试告诉 gunicorn 绑定到所有接口:

--bind 0.0.0.0:8000

然后在 nginx 配置中,通过 HTTP 而不是套接字传递给它:

proxy_pass http://localhost:8000/

【讨论】:

  • 按照您说的完成,效果更好。现在我可以访问应用程序,但它会不断重启。在 error.log 我看到:2020/08/25 20:57:59 [error] 4100#4100: *14 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.178, server: 192.168.0.151, request: "GET /profile?id=2&user_name=piotrek HTTP/1.1", upstream: "http://[::1]:8000/profile?id=2&user_name=piotrek", host: "192.168.0.151", referrer: "http://192.168.0.151/profile?id=2&user_name=piotrek"
  • @gawron103 Em。这看起来 like this 与 IPv6 请求有关。尝试改用proxy_pass http://127.0.0.1:8000/,专门让它使用IPv4传递到上游。
猜你喜欢
  • 2018-08-03
  • 2023-03-20
  • 2020-11-28
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 2016-04-20
  • 2020-09-01
  • 2017-04-13
相关资源
最近更新 更多