使用 flask_socketio + flask + gunicorn + nginx 获取 502 错误网关和 400 错误请求

Getting 502 bad gateway and 400 bad request with flask_socketio + flask + gunicorn + nginx

所以我一直在用flask + flask_socketio + eventlet + gunicorn + nginx搭建一个网站。我已经查看了此配置的 flask_socketio 文档,它是受支持的。虽然在使用该网站后我注意到虽然 flask 可以完美运行,但 flask_socketio 根本无法运行。下面是相关的配置和错误代码。我想指出一些 flask_socketio 位 post 请求会通过 200 OK。
这是在 ubuntu 18.04
Nginx:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 65535;
    multi_accept on;
}

http {

    ##
    # Basic Settings
    ##
    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    log_not_found off;
    client_max_body_size 16M;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##
server {
    listen 80;
    server_name http://compoundingly.com;

    location / {
        proxy_pass http://0.0.0.0:5000;
    proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /static {
        alias /home/shorya/compoundingly_website/compoundingly_code/static;
        expires 30d;
    }
    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://0.0.0.0:5000/socket.io;
    }
}

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}



Gunicorn 服务

[Unit]
Description=Gunicorn instance to serve myproject
Requires=gunicorn.socket
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/home/shorya/compoundingly_website/compoundingly_code
Environment="PATH=/home/shorya/compoundingly_website/compoundingly_code/compoundingly_venv/bin"
ExecStart=/home/shorya/compoundingly_website/compoundingly_code/compoundingly_venv/bin/gunicorn -k eventlet --workers 3 --bind unix:compoundingly_code.sock -m 007 wsgi:app
Restart=on-failure
[Install]
WantedBy=multi-user.target

wsgi.py

from app import app

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

pip3 冻结

click==7.1.2
dnspython==1.16.0
eventlet==0.26.1
Flask==1.1.2
Flask-SocketIO==4.3.1
greenlet==0.4.16
gunicorn==19.7.1
itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
monotonic==1.5
pkg-resources==0.0.0
python-engineio==3.13.1
python-socketio==4.6.0
six==1.15.0
Werkzeug==1.0.1

请求失败

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwVcP&sid=22ad9ddeaffe4eeb8525927aca86806b
Status: 400 BAD REQUEST
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

好请求

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwVtd
Status: 200 OK
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

502 错误的网关请求

URL: http://compoundingly.com/socket.io/?EIO=3&transport=polling&t=NFGwdji&sid=69f16b45a1aa474294cdeb2452f0e71a
Status: 502 Bad Gateway
Source: Network
Address: 192.241.149.99:80
Initiator: 
index.js:83

Gunicorn 错误日志


Aug 08 22:09:16 sm01 gunicorn[1956]: [2020-08-08 22:09:16 -0400] [5964] [INFO] Booting worker with pid: 5964
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5962)
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [5962] [INFO] Worker exiting (pid: 5962)
Aug 08 22:09:47 sm01 gunicorn[1956]: [2020-08-08 22:09:47 -0400] [5977] [INFO] Booting worker with pid: 5977
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5963)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [1956] [CRITICAL] WORKER TIMEOUT (pid:5964)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5963] [INFO] Worker exiting (pid: 5963)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5964] [INFO] Worker exiting (pid: 5964)
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5981] [INFO] Booting worker with pid: 5981
Aug 08 22:09:48 sm01 gunicorn[1956]: [2020-08-08 22:09:48 -0400] [5982] [INFO] Booting worker with pid: 5982

对于遇到同样问题的人,在完成此操作后,您可以执行一些操作来使系统正常工作: 我将 wsgi 更改为:

from app import *

if __name__ == "__main__":
    #app.run()
    app = Flask(__name__)#,template_folder="./dist/app/templates"
    app.config['SECRET_KEY'] = 'secret!'
    socketio = SocketIO(app,async_mode="eventlet")
    socketio.run(app,host="0.0.0.0",port=5000,debug=True)

添加 --bind 0.0.0.0:5000 到 gunicorn 命令。
我还下载了更新版本的 gunicorn 但不是最新的。
19.7 不工作
20.0.3 有效
20.0.4 不工作

请参阅 documentation 了解如何 运行 Gunicorn with eventlet。特别是,您正在使用不支持的多个工作器。

文档中支持的示例配置:

gunicorn --worker-class eventlet -w 1 module:app