4

我正在尝试使用 flask-socketio 运行我的烧瓶应用程序。在更改了许多配置后,我终于设法在 aws 上的 ubuntu 服务器上使用 gunicorn、nginx 和 gevent 运行了我的烧瓶应用程序。但是每当我尝试连接时,连接都会失败,并且在错误日志中我会收到 Invalid session 错误。我也安装了 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;
                }
}

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

使用gunicorn -k gevent -w 1 运行应用程序后更新 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
4

0 回答 0