![]() |
|
Guide WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs - Printable Version +- AdminsForum (https://adminsforum.com) +-- Forum: Free & open-source control panels (https://adminsforum.com/Forum-free-open-source-control-panels) +--- Forum: CyberPanel (https://adminsforum.com/Forum-cyberpanel) +---- Forum: CyberPanel Guides & Tutorials (https://adminsforum.com/Forum-cyberpanel-guides-tutorials) +----- Forum: CyberPanel Websites & OpenLiteSpeed Guides (https://adminsforum.com/Forum-cyberpanel-websites-openlitespeed-guides) +----- Thread: Guide WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs (/Thread-guide-websockets-on-cyberpanel-the-extra-rewrite-rule-your-node-js-app-needs) |
WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs - JAY - 11-14-2026 WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs (Tested) Your Node.js app runs behind a CyberPanel website and normal pages load fine, but the live part (chat, notifications, socket.io) never connects. That's what happened in our test too: the usual proxy rule passes web pages but not WebSocket connections. One extra rule fixes it. We tested this on a CyberPanel 3.0.8 server with a small WebSocket app. This guide continues our Node.js on CyberPanel guide. Set the app up with that one first. Which web server: tested on LiteSpeed Enterprise only. OpenLiteSpeed handles WebSocket proxying with its own setting in the site's vHost Conf, not with this rule. The problem Our app listened on Code: 127.0.0.1:3100Code: RewriteEngine On
RewriteRule ^(.*)$ http://127.0.0.1:3100/$1 [P,L]Code: ws://127.0.0.1:3100/chat OPEN (straight to the app, on the server)
wss://yourdomain.com/chat TIMEOUT (through the website)The fix: one more rule Open Websites → your site → Config → Rewrite Rules and use these four lines instead: Code: RewriteEngine On
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteRule ^(.*)$ ws://127.0.0.1:3100/$1 [P,L]
RewriteRule ^(.*)$ http://127.0.0.1:3100/$1 [P,L]Lines 2 and 3 are new. A WebSocket connection starts as a normal request that asks to be "upgraded". Line 2 spots that request, and line 3 passes it on with Code: ws://Code: http://Use Code: ws://Code: wss://Our result after the change Code: wss://yourdomain.com/chat OPEN
got: welcome
got: echo: ping 1
got: echo: ping after 3sTest your own app You can test from the server with a few lines of Node.js (needs the Code: wsCode: wstest.jsCode: const WebSocket = require('ws');
const ws = new WebSocket(process.argv[2]);
setTimeout(() => { console.log('TIMEOUT'); process.exit(1); }, 8000);
ws.on('open', () => { console.log('OPEN'); ws.send('hello'); });
ws.on('message', (m) => { console.log('got: ' + m); });
ws.on('error', (e) => { console.log('ERROR ' + e.message); process.exit(1); });Code: node wstest.js wss://yourdomain.com/your-socket-pathInstalling packages as the website's user We installed the Code: wsCode: cd /home/yourdomain.com/nodeapp
sudo -u SITEUSER npm install ws
systemctl restart nodeappCode: node_modulesGood to know
Still stuck? Reply below with the result of the test script and the last lines of Code: journalctl -u nodeapp |