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

Socket.io经反向代理后持续轮询,无法正常建立连接求助

Hey there! I’ve run into this exact issue a few times when setting up Socket.io behind a reverse proxy—super frustrating when everything works flawlessly locally but grinds to a halt once you add the proxy layer. Let’s break down the most common missing configs and fixes that usually resolve this long-polling loop problem.

Key Fixes for Socket.io Reverse Proxy Issues

The root cause here is almost always that your proxy isn’t properly handling WebSocket connections, so Socket.io falls back to long polling (and gets stuck there since the proxy isn’t relaying data correctly). Here’s how to fix it for the most popular proxies:

Nginx (Most Common Scenario)

Add this location block to your Nginx server config—this tells Nginx to pass through WebSocket handshakes and maintain the connection:

location /socket.io/ {
    proxy_pass http://your-app-internal-ip:your-app-port; # Replace with your app's actual address
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

The critical lines here are proxy_http_version 1.1 (WebSocket requires HTTP/1.1) and the Upgrade/Connection headers—these signal to Nginx that it should treat this as a WebSocket connection instead of a regular HTTP request.

On the client side, make sure you’re connecting with the right protocol (use wss:// if your proxy serves HTTPS) and prioritize WebSocket over polling:

const socket = io({
    path: "/socket.io/",
    transports: ["websocket", "polling"] // Try WebSocket first, fall back only if needed
});

Apache

First, enable the required modules:

a2enmod proxy proxy_http proxy_wstunnel

Then add this to your virtual host config to route WebSocket traffic correctly:

# For HTTP proxies
ProxyPass /socket.io/ ws://your-app-internal-ip:your-app-port/socket.io/
ProxyPassReverse /socket.io/ ws://your-app-internal-ip:your-app-port/socket.io/

# For HTTPS proxies
ProxyPass /socket.io/ wss://your-app-internal-ip:your-app-port/socket.io/
ProxyPassReverse /socket.io/ wss://your-app-internal-ip:your-app-port/socket.io/

Other Common Pitfalls to Check

  • CORS Misconfiguration: Even if the proxy works, your Socket.io server might block requests from your proxy’s domain. Update your server’s CORS settings:
    const io = require('socket.io')(server, {
        cors: {
            origin: "https://your-proxy-domain.com", // Replace with your public proxy URL
            methods: ["GET", "POST"]
        }
    });
    
  • Proxy Timeouts: Long polling needs longer timeouts to avoid premature connection drops. For Nginx, add these lines to your location block:
    proxy_connect_timeout 7d;
    proxy_send_timeout 7d;
    proxy_read_timeout 7d;
    
  • Incorrect Path: Make sure the path in your client matches the one your proxy is routing—if your server uses a custom path for Socket.io, adjust both the proxy and client configs to match.

Start with the WebSocket upgrade config for your proxy—that’s the fix that resolves this issue 9 times out of 10. If that doesn’t work, double-check CORS and client transport settings.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:12:32