Node.js服务器WebSocket部署疑问:GCP虚拟机端口配置及静态站点对接
Hey Andrew, let's get your IO game's WebSocket connection up and running between your GCP VM and static frontend on gowar.io. I'll break this down into actionable, easy-to-follow steps:
1. Open Your VM's Port & Configure GCP Firewall
First, GCP's default firewall blocks incoming traffic to most ports—so we need to open port 3000 (or switch to standard ports like 80/443 later) for external access:
- Head to your GCP Console > VPC Network > Firewall
- Create a new firewall rule:
- Name it something like
allow-socketio-traffic - Set Target tags to match the tag assigned to your VM (add a tag to your VM if it doesn't have one)
- Under Source IP ranges, enter
0.0.0.0/0(or restrict to your domain's IPs for extra security) - In Protocols and ports, check
tcpand enter3000
- Name it something like
- Save the rule—this lets external traffic reach your VM's port 3000.
2. Update Your Server Code to Listen Externally
Right now, your server is only listening on localhost, which means it won't accept connections from outside the VM. Tweak your code:
var express = require('express'); var app = express(); // Listen on all network interfaces (0.0.0.0) instead of just localhost var server = app.listen(3000, '0.0.0.0', () => { console.log("Server Has Started and is accessible externally on port 3000"); }); app.use(express.static('public')); var socket = require('socket.io'); // Configure CORS to allow requests from your gowar.io domain var io = socket(server, { cors: { origin: "https://gowar.io", // Use your actual frontend URL (include https!) methods: ["GET", "POST"] } });
This fixes two key issues: external accessibility and cross-origin requests (since your frontend is on gowar.io and your server is on a different IP/host).
3. Update Client-Side Connection URL
Replace the localhost URL in your client code with your VM's external IP address (or a custom subdomain like api.gowar.io if you set that up later):
// Use your VM's external IP (find it in GCP VM details) var socket = io.connect("https://your-vm-external-ip:3000"); // Or if you set up a subdomain: var socket = io.connect("https://api.gowar.io");
⚠️ Important: If gowar.io uses HTTPS (which it should), your Socket.io server needs to use HTTPS too—otherwise browsers will block the mixed content. I'll cover that in the optional step below.
4. (Optional) Use Nginx for Reverse Proxy & HTTPS
For a production-ready setup, use Nginx to:
- Map your subdomain (like
api.gowar.io) to your VM's port 3000 - Handle HTTPS with a free Let's Encrypt certificate
- Avoid exposing port 3000 directly
Here's a quick Nginx config snippet for this:
server { listen 443 ssl; server_name api.gowar.io; # SSL certs from Let's Encrypt ssl_certificate /etc/letsencrypt/live/api.gowar.io/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/api.gowar.io/privkey.pem; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; } }
After setting this up, your client can connect with var socket = io.connect("https://api.gowar.io") (no port needed!)
5. Test the Connection
- First, SSH into your VM and start your Node server
- Visit gowar.io, open your browser's dev tools > Network tab > check for a successful WebSocket connection (look for a
wss://request) - If you see errors, double-check:
- Firewall rule is active and targeting your VM
- Server is listening on
0.0.0.0 - CORS settings match your frontend URL
- HTTPS is configured if your frontend uses HTTPS
内容的提问来源于stack exchange,提问作者Andrew Hansen

