首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gunicorn + nginx 502 Bad Gateway上的Flask应用

Gunicorn + nginx 502 Bad Gateway上的Flask应用
EN

Stack Overflow用户
提问于 2020-08-26 02:29:11
回答 1查看 506关注 0票数 1

我有一个问题,我的flask应用程序使用nxginx和gunicorn。当我检查nginx状态时,它看起来正常:

代码语言:javascript
复制
● 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看起来也不错:

代码语言:javascript
复制
● 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是我的桌面):

代码语言:javascript
复制
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停止nginx和gunicorn并运行gunicorn --bind 192.168.0.151 -w 5 wsgi:app时,我的应用程序是活跃的,并且可以从桌面访问,但它每隔20秒就会重新启动一次。

Gunicorn配置:

代码语言:javascript
复制
[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配置:

代码语言:javascript
复制
server {
    listen 80;
    server_name 192.168.0.151;

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

wsgi.py

代码语言:javascript
复制
from server import create_app

app = create_app()

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

EN

回答 1

Stack Overflow用户

发布于 2020-08-26 02:42:45

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

--bind 0.0.0.0:8000

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

代码语言:javascript
复制
proxy_pass http://localhost:8000/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63585041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档