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

配置Nginx适配Socket.io时出现502 Bad Gateway错误求助

Fixing Socket.io 502 Bad Gateway Error with Nginx Reverse Proxy

Hey there, let's work through this 502 Bad Gateway issue you're hitting when deploying your Node/Express/Socket.io app. I've run into this exact problem before, so let's break down the fixes step by step.

First, Verify the Basics

Before tweaking Nginx, let's make sure your Node service is actually working correctly:

  • SSH into your server and run node index.js to start your app.
  • From the server itself, run curl http://127.0.0.1:3000/socket.io/socket.io.js — if you get a valid JavaScript file back, your Node/Socket.io setup is good. If not, double-check your index.js code (your current setup looks correct, but maybe a dependency is missing? Run npm install again to be sure).

Fix Your Nginx Configuration

The 502 error usually means Nginx can't properly forward the Socket.io traffic to your Node server. Let's update your Nginx config with some critical missing settings:

server {
  listen 80;
  server_name sub.domain.com;

  # Handle general app traffic
  location / {
    proxy_pass http://127.0.0.1:3000;
    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_connect_timeout 30s;
    proxy_send_timeout 30s;
    proxy_read_timeout 30s;
  }

  # Handle Socket.io WebSocket traffic
  location /socket.io/ {
    proxy_pass http://127.0.0.1:3000/socket.io/;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    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_cache_bypass $http_upgrade; # Bypass cache for WebSocket requests
    proxy_connect_timeout 7d; # Longer timeout for persistent WebSocket connections
  }
}

Key Changes Made:

  • Added timeout settings to prevent premature connection drops
  • Added X-Real-IP and X-Forwarded-For headers so your app can see the actual client IP
  • Added proxy_cache_bypass to ensure WebSocket traffic isn't cached
  • Increased the proxy_connect_timeout for WebSocket connections (they're persistent, so short timeouts cause issues)

Post-Config Steps

  1. Test your Nginx config to avoid syntax errors: sudo nginx -t
  2. Restart Nginx to apply changes: sudo systemctl restart nginx
  3. Use a process manager for your Node app (like PM2) to ensure it stays running in production:
    • Install PM2: npm install -g pm2
    • Start your app: pm2 start index.js
    • Save the PM2 process list: pm2 save
    • Set PM2 to start on boot: pm2 startup

Quick Optimization Note

Your frontend is referencing /node_modules/jquery/dist/jquery.js — in production, it's better to serve static files properly instead of exposing your node_modules folder. Add this line to your index.js to serve static assets:

app.use(express.static('public'));

Then move your jQuery file (or use a CDN version) into a public folder, and update the script tag to /jquery.js. This is more secure and performant.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:14:05