使用 flask_socketio + flask + gunicorn + nginx 获得 502 bad gateway 和 400 bad request
Posted
技术标签:
【中文标题】使用 flask_socketio + flask + gunicorn + nginx 获得 502 bad gateway 和 400 bad request【英文标题】:Getting 502 bad gateway and 400 bad request with flask_socketio + flask + gunicorn + nginx 【发布时间】:2020-11-28 23:26:21 【问题描述】:所以我一直在用flask + flask_socketio + eventlet + gunicorn + nginx 建立一个网站。我已经查看了该配置的 flask_socketio 的文档,并且它是受支持的。虽然在使用该网站后,我注意到虽然 flask 可以完美运行,但 flask_socketio 根本无法运行。以下是相关配置和错误代码。我想指出,一些 flask_socketio 位发布请求将通过 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)
在 gunicorn 命令中添加了 --bind 0.0.0.0:5000。 我还下载了较新版本的 gunicorn,但不是最新版本。 19.7 不工作 20.0.3 作品 20.0.4 不行
【问题讨论】:
您是否使用flask-socketio 来设置您的服务器?我没有看到你在 wsgi.py 中使用过它的模块 谢谢,这是我没有真正注意到的。 不客气。如果您在使用 flask-socketio 时遇到问题,请随时问我 【参考方案1】:请参阅documentation 了解如何使用 eventlet 运行 Gunicorn。特别是,您正在使用多个工作人员,这是不受支持的。
文档中支持的配置示例:
gunicorn --worker-class eventlet -w 1 module:app
【讨论】:
非常感谢您的回答。我非常感谢您对支持您已经创建的东西的承诺。以上是关于使用 flask_socketio + flask + gunicorn + nginx 获得 502 bad gateway 和 400 bad request的主要内容,如果未能解决你的问题,请参考以下文章
以设定的时间间隔从 flask_socketio 服务器发出事件