无法连接到 Flask-socketio,会话 ID 错误无效

kar*_*han 5 nginx gevent flask gunicorn flask-sockets

我正在尝试使用 Flask-socketio 运行我的 Flask 应用程序。在更改了许多配置之后,我终于成功地在 aws 上的 ubuntu 服务器上使用 Gunicorn、nginx 和 gevent 运行我的 Flask 应用程序。但每当我尝试连接时,连接都会失败,并且在错误日志中出现“无效会话”错误。我也安装了 gevent-websockets 但它也不起作用。这是错误消息。

[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"
Run Code Online (Sandbox Code Playgroud)

这是我的 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;
                }
}
Run Code Online (Sandbox Code Playgroud)

这些是正在运行的实例。 在此输入图像描述

使用运行应用程序后更新 1gunicorn -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. '
Run Code Online (Sandbox Code Playgroud)

更新2

根据文档

当使用gunicorn与gevent工作器和gevent-websocket提供的WebSocket支持时,必须更改启动服务器的命令以选择支持WebSocket协议的自定义gevent Web服务器。修改后的命令为:

gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker -w 1 module:app
Run Code Online (Sandbox Code Playgroud)