You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx+uWSGI+Flask-SocketIO配置下WebSocket连接报错排查求助

Hey there! It looks like you didn’t include your actual uWSGI and Nginx config snippets in your question yet—those are critical for pinpointing exactly what’s breaking your Socket.IO/WebSocket connection. That said, I can walk you through the most common pitfalls and share a tested working config pair you can compare against your setup.

Common Config Red Flags to Check
  • uWSGI WebSocket Support: uWSGI needs explicit WebSocket support enabled. If you compiled uWSGI from source, make sure you used the --enable-websockets flag. In your uWSGI config, add http-websockets = true to turn on this functionality.
  • Nginx Header Handling: WebSocket connections rely on the Upgrade and Connection headers to negotiate the handshake. If Nginx isn’t passing these through correctly, the connection will fail.
  • Async Mode Mismatch: Flask-SocketIO requires an async server like gevent or eventlet. Your uWSGI config must use one of these, and your Flask app needs to specify the matching async_mode.
  • Version Compatibility: Double-check that your frontend Socket.IO client version matches your backend Flask-SocketIO version (e.g., client v4.x pairs with Flask-SocketIO v5.x). Mismatches often cause silent failures or connection errors.
Example Working Configs

uWSGI Config (app.ini)

[uwsgi]
# Point to your Flask app's WSGI entry point
module = wsgi:app
master = true
# Use 1 process with multiple threads/gevent greenlets (Socket.IO works best with this setup)
processes = 1
threads = 10
gevent = 1000
# Enable WebSocket support
http-websockets = true
# Unix socket for Nginx communication
socket = /tmp/uwsgi_app.sock
chmod-socket = 660
vacuum = true
die-on-term = true

Nginx Config Snippet

server {
    listen 80;
    server_name your-domain.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi_app.sock;

        # Mandatory for WebSocket handshake
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        
        # Pass through necessary request headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Long timeout for persistent Socket.IO connections
        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }

    # If your Socket.IO uses a custom path (e.g., /socket.io/), add this block
    location /socket.io/ {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi_app.sock;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_connect_timeout 7d;
        proxy_send_timeout 7d;
        proxy_read_timeout 7d;
    }
}
Flask App Setup Note

Make sure your Flask-SocketIO instance uses the correct async mode to match uWSGI:

from flask import Flask
from flask_socketio import SocketIO

app = Flask(__name__)
# Match this to the async server you're using in uWSGI (gevent here)
socketio = SocketIO(app, async_mode='gevent')

# Your routes and Socket.IO event handlers go here

if __name__ == '__main__':
    socketio.run(app)

Once you share your actual config files, we can dig into more specific issues. But start with verifying these points—they’re the most common causes of Socket.IO connection failures with uWSGI and Nginx.

内容的提问来源于stack exchange,提问作者Austen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:49:57