Love it — turning this into a blog is a great idea 👍
I’ll write it clean, structured, and copy-paste ready.
You can publish this directly as a guide.
🚀 Deploying Socket.io with FastPanel + Nginx Reverse Proxy + Cloudflare
Complete production setup guide
This guide explains how to deploy a Node.js Socket.io server on a FastPanel VPS and expose it securely using:
FastPanel reverse proxy
Nginx WebSocket upgrade
Cloudflare SSL
Subdomain routing
Final result:
willcart.in → CodeIgniter (main app)
socket.willcart.in → Node Socket Server
Both run independently.
📌 Architecture Overview
Browser (HTTPS / WSS)
↓
Cloudflare
↓
FastPanel nginx reverse proxy
↓
Node socket server (port 3000)
Node runs internally on HTTP.
SSL is handled by nginx + Cloudflare.
✅ Step 1 — Create socket subdomain in FastPanel
Open FastPanel:
Websites → Add Website
Choose template:
👉 Reverse proxy
Domain:
socket.willcart.in
IP:
your server IP
Save.
✅ Step 2 — Configure reverse proxy backend
Open:
socket.willcart.in → Settings → Backend
Add backend:
Type: host
Address: http://127.0.0.1:3000
Save.
This forwards traffic to Node server running on port 3000.
✅ Step 3 — Enable SSL
Open:
socket.willcart.in → HTTPS
Enable:
👉 Let’s Encrypt SSL
Now your socket server is accessible via:
https://socket.willcart.in
✅ Step 4 — Fix CORS in Socket.io
Without this, browser blocks cross-domain requests.
Edit Node server:
❌ Old
var io = require('socket.io')(http);
✅ Replace with
var io = require('socket.io')(http, {
cors: {
origin: ["https://willcart.in"],
methods: ["GET", "POST"],
credentials: true
}
});
Restart Node:
pm2 restart all
✅ Step 5 — Enable WebSocket upgrade in nginx
FastPanel does not always enable WebSocket headers automatically.
Open nginx config:
nano /etc/nginx/sites-enabled/socket.willcart.in.conf
Find proxy block:
location / {
proxy_pass http://127.0.0.1:3000;
}
Replace with:
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Also update fallback block:
location @fallback {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Reload nginx:
nginx -t
systemctl reload nginx
✅ Step 6 — Frontend connection
Use HTTPS socket endpoint:
const socket = io("https://socket.willcart.in", {
transports: ["websocket"]
});
Never expose port 3000 publicly.
✅ Step 7 — Test
Open browser console:
Connected!
If you see this, setup is complete.
🎯 Common errors and fixes
CORS error
blocked by CORS policy
👉 Fix Step 4
WebSocket connection failed
wss://... failed
👉 Fix Step 5
ERR_EMPTY_RESPONSE
Trying to access:
:3000
Port is private. Use domain only.
🛡 Production best practices
Recommended improvements:
Use PM2 cluster mode
Enable auto restart
Add JWT socket authentication
Add rate limiting
Monitor memory usage
Enable Cloudflare WebSocket support
✅ Final working setup
Main site:
https://willcart.in
Socket server:
https://socket.willcart.in
Both run independently without conflict.
0 comments:
Post a Comment