我正在尝试使用flask-socketio运行我的flask应用程序。更改许多配置后,我终于设法在AWS的ubuntu服务器上用gunicorn,nginx和gevent运行flask应用程序。但是,每当我尝试连接时,连接都会失败,并且在错误日志中,我会收到无效的会话错误。 我也安装了gevent-websokets,但效果不佳。 这是错误消息
[29/06/2020 03:50:28.388|15022|WARNING|engineio.server |server.py:391 handle_request ]: Invalid session 90ff42cdbd944b07ad2c2f6f484ff5a3
[29/Jun/2020:03:50:28 +0500] 127.0.0.1 "POST /socket.io/?EIO=3&transport=polling&t=NBz7-Tz&sid=90ff42cdbd944b07ad2c2f6f484ff5a3 HTTP/1.1" 400 11 "-" "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
这是我的Nginx配置
server {
listen 80;
server_name my_app.com;
location / {
proxy_pass "http://localhost:5000";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
fastcgi_read_timeout 300s;
proxy_read_timeout 300;
}
location /static {
alias /opt/deployment/my-api-app/static/;
}
error_log /var/log/nginx/api-error.log;
access_log /var/log/nginx/api-access.log;
location /socket.io {
include /etc/nginx/proxy_params;
proxy_http_version 1.1;
proxy_buffering off;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass http://127.0.0.1:5000/socket.io;
}
}
更新1 使用 gunicorn -k gevent -w 1 运行应用程序后,出现此错误
Traceback (most recent call last):
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/base_async.py", line 55, in handle
self.handle_request(listener_name, req, client, addr)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/ggevent.py", line 143, in handle_request
super().handle_request(listener_name, req, sock, addr)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/gunicorn/workers/base_async.py", line 106, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/flask_socketio/__init__.py", line 46, in __call__
start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/middleware.py", line 60, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/socketio/server.py", line 560, in handle_request
return self.eio.handle_request(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/server.py", line 377, in handle_request
environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/socket.py", line 108, in handle_get_request
start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/socket.py", line 152, in _upgrade_websocket
return ws(environ, start_response)
File "/home/ubuntu/app-pyvenv/lib/python3.5/site-packages/engineio/async_drivers/gevent.py", line 35, in __call__
raise RuntimeError('You need to use the gevent-websocket server. '
更新2
根据文档
在将gunicorn与gevent worker和gevent-websocket提供的WebSocket支持一起使用时,必须更改启动服务器的命令,以选择支持WebSocket协议的自定义gevent Web服务器。修改后的命令是:
gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app