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-websocketsflag. In your uWSGI config, addhttp-websockets = trueto turn on this functionality. - Nginx Header Handling: WebSocket connections rely on the
UpgradeandConnectionheaders 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
geventoreventlet. Your uWSGI config must use one of these, and your Flask app needs to specify the matchingasync_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
相关产品推荐
相关产品推荐

