带有nginx + uwsgi + flask-socketio的WebSockets

时间:2018-03-06 21:41:44

标签: python nginx websocket uwsgi flask-socketio

我正在尝试使用前端的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 个答案:

答案 0 :(得分:1)

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