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

WebSocket本地正常但远程Ubuntu主机连接失败求助

Troubleshooting Colyseus WebSocket Handshake Failure on Ubuntu VPS

Hey there, let's work through why your browser-based client can't connect to your Colyseus Node.js server on Ubuntu VPS—since it works locally, the issue is almost certainly tied to deployment-specific settings. Here's what to check step by step:

1. Firewall & Cloud Security Group Rules

This is the #1 culprit for connection reset errors. Ubuntu uses ufw by default, so first make sure port 8001 is open:

# Allow incoming TCP traffic on port 8001
sudo ufw allow 8001/tcp
# Verify the rule is active
sudo ufw status

Don't forget your VPS provider's cloud-level security group! Platforms like DigitalOcean, AWS, or Alibaba Cloud have their own firewalls separate from Ubuntu's ufw. Log into your provider's console and add an inbound rule to allow TCP traffic on port 8001 from all IPs (or your client's IP for testing).

2. Server Binding Address

Local tests often bind to localhost or 127.0.0.1, which only allows connections from the same machine. On your VPS, you need to bind the Colyseus server to 0.0.0.0 to accept external connections:

const { Server } = require("colyseus");
const http = require("http");

const app = require("express")();
const gameServer = new Server({ server: http.createServer(app) });

// Make sure to specify 0.0.0.0 here
gameServer.listen(8001, "0.0.0.0");
console.log("Colyseus server running on ws://0.0.0.0:8001");

If you only call gameServer.listen(8001), some Node.js environments default to binding localhost, which blocks external requests.

3. NGINX Reverse Proxy Configuration (If Used)

If you're using NGINX to proxy traffic to your Colyseus server, missing WebSocket-specific headers will cause handshake failures. Update your NGINX site config to include these lines:

server {
    listen 80;
    server_name your-domain.com; # or your VPS IP

    location / {
        proxy_pass http://localhost:8001;
        # Required for WebSocket handshake
        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;
    }
}

After updating, reload NGINX with sudo systemctl reload nginx to apply changes.

4. Port Accessibility Test

Verify that the port is actually reachable from outside the VPS. On your local machine, run:

# Using netcat
nc -zv X.X.X.X 8001
# Or telnet
telnet X.X.X.X 8001

If the connection is refused or times out, go back to step 1 to double-check firewall/security group rules.

5. Dependency & Version Mismatches

Even if it works locally, mismatched Node.js or Colyseus versions on the VPS can cause unexpected issues. Run these commands on both your local machine and VPS to compare:

# Check Node.js version
node -v
# Check Colyseus version
npm list colyseus

If versions differ, update the VPS to match your local environment. You can also try reinstalling dependencies to clear cached issues:

rm -rf node_modules package-lock.json
npm install

Start with the firewall and security group checks first—those are the most frequent fixes for this exact error. Let me know if any of these steps resolve your issue!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:26:10