使用nginx + uwsgi + flask-socketio实现WebSockets

3

我正在尝试使用socket.io在前端和Flask-Socketio在后端建立websocket连接。然而,前端出现了错误。

WebSocket connection to 'ws://myserver.com/socket.io/?EIO=3&transport=websocket&sid=0514a0aa99f346e7ad717770f9911c89' failed: WebSocket is closed before the connection is established.

我有一种感觉,这可能是由于我的uwsgi或nginx配置引起的。
以下是我正在使用的uwsgi配置:
[uwsgi]
base = /var/www/webapp
file = %(base)/run.py
callable = app
pythonpath = %(base)
socket = /tmp/uwsgi.sock
chmod-socket = 666
http-websockets = true
gevent = 1000
processes = 1
threads = 2
enable-threads = true
single-interpreter = true
master = true
chdir = /var/www/webapp
fs-reload = %(base)/app/
touch-reload = %(base)/run.py
py-autoreload = 1
harakiri = 3600

以下是nginx配置文件中的相关部分:

    location /socket.io/ {
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/.htpasswd;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }

你是否在使用

socket = /tmp/uwsgi.sock

从uwsgi配置文件中

uwsgi_pass unix:/tmp/uwsgi.sock;

从nginx配置文件中获取吗?非常感谢您的帮助。当然,如果需要提供更多信息,请告知。


1
尝试将线程从两个切换到一个。不确定这是否是问题,但我从未打算让此服务器成为多线程的,您可以通过greenlets实现多任务处理。 - Miguel Grinberg
你解决了这个问题吗? - MistyD
@Miguel,我改成单线程后问题解决了。 - alquist42
1个回答

2

您应该将uwsgi_pass unix:/tmp/uwsgi.sock;更改为uwsgi_pass unix:///tmp/uwsgi.sock;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接