AdminsForum

CyberPanel Websites & OpenLiteSpeed Guides

Guide WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs

Started by JAY 0 replies 128 views
1JAY
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#1
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:3100
, and the website passed everything to it with the rule from the Node.js guide:
Code:
RewriteEngine On RewriteRule ^(.*)$ http://127.0.0.1:3100/$1 [P,L]
We then tested a WebSocket connection two ways:
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 confusing part: the app's own log said WebSocket client connected for both. The request reached the app, but the answer never made it back to the visitor, so the browser side just waits and gives up.

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]
cyberpanel rewrite rules websocket proxy   
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://
instead of
Code:
http://
, which tells LiteSpeed to keep the connection open in both directions. Everything else still goes through line 4.

Use
Code:
ws://
in the rule even though visitors connect with
Code:
wss://
. The website handles the encryption, and the hop to your app on the same server is plain.

Our result after the change
Code:
wss://yourdomain.com/chat OPEN   got: welcome   got: echo: ping 1   got: echo: ping after 3s
The connection opened, messages went both ways, and it was still alive when we sent another message three seconds later. Normal pages kept loading as before. It worked right after saving the rules.

Test your own app
You can test from the server with a few lines of Node.js (needs the
Code:
ws
package, see below). Save as
Code:
wstest.js
in your app folder:
Code:
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); });
Run it against your domain:
Code:
node wstest.js wss://yourdomain.com/your-socket-path
OPEN within a second or two means the rule works. TIMEOUT means the WebSocket rule is missing or not matching.

Installing packages as the website's user
We installed the
Code:
ws
package as the website's own user, not as root, so the files belong to the right owner:
Code:
cd /home/yourdomain.com/nodeapp sudo -u SITEUSER npm install ws systemctl restart nodeapp
That worked on our server and created
Code:
node_modules
owned by the site user.

Good to know
  • socket.io: it uses the same kind of connection, usually under the path
    Code:
    /socket.io/
    . Our rule matches every path, so it's covered. We tested with a plain WebSocket app, not with socket.io itself.
  • Long idle connections: our test ran for a few seconds. If connections drop after a minute or two of silence, have your app send a small "ping" message regularly. Most WebSocket libraries have a setting for it.
  • Behind Cloudflare: WebSockets also have to be allowed there (they are by default). We tested without Cloudflare in front.

Still stuck? Reply below with the result of the test script and the last lines of
Code:
journalctl -u nodeapp
(hide your domain and server IP).
Reply

Messages In This Thread
WebSockets on CyberPanel: The Extra Rewrite Rule Your Node.js App Needs - by JAY - 11-14-2026, 05:58 PM
Possibly Related Threads…matched by title
Replies:0Views:125
Replies:0Views:132
Replies:0Views:140
Replies:0Views:134
Replies:0Views:130

Users browsing this thread: 1 Guest(s)
Forum Jump: