Websocket server on Phusion Passenger with Apache

129 Views Asked by At

I have a VPS with WHM/CPanel where i have Phusion Passenger running with Node.js apps. For http(s) this works great and i dont have any problems. I am using it as defined in the manuals:

if (typeof(PhusionPassenger) !== 'undefined') {
  server = app.listen('passenger', () => {
    this.log('Express.js-server started via Passenger');
  });
} else {
  server = app.listen(process.env.HTTP_PORT, () => {
    this.log(`Express.js-server started on port ${process.env.HTTP_PORT}`);
  });
}

This starts a http server and i can access it fine. Now i want to add a WebSocket server so i added this

wss = new WebSocket.Server({ noServer: true });
// Handle the upgrade event for the WebSocket server.
server.on('upgrade', (request, socket, head) => {

  socket.on('error', (err) => {
    console.log(`WebSocket error: ${err}`);
  });

  const clientIp = request.headers['x-forwarded-for'] || request.connection.remoteAddress;
  wss.handleUpgrade(request, socket, head, (ws) => {
    ws.clientIp = clientIp;
    console.log('WebSocket upgrade complete: ' + clientIp);
    wss.emit('connection', ws, request);
  });
});

When i try to connect to the webscocket server and i check the logs i can see that the "WebSocket upgrade complete: xx.xx.xx.xx" is logged, and also the log in the connected event is logged - so it looks like the server accepted the upgrade from http(s) to websocket.

But the client keeps saying it cant connect, i tried various chrome websocket plugins, and i tried to create my own websocket client - but i cant connect. When i look at the server i can see the websocket connection on the server is not closed yet - but the client is not connected..

I found alot of posts about Phusion Passenger and Websockets, and they state that i cant use Apache with Phusion Passenger and websockets, but these are all 8-10yrs old posts; I already tried to enable proxying in apache using these configurations:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so


<VirtualHost *:80>
  RewriteEngine on
  RewriteCond ${HTTP:Upgrade} websocket [NC]
  RewriteCond ${HTTP:Connection} upgrade [NC]
  RewriteRule .* "wss://webos.erdesigns.dev/$1" [P,L]

  ProxyPass / wss://webos.erdesigns.dev/
  ProxyPassReverse / wss://webos.erdesigns.dev/
  ProxyRequests off
</VirtualHost>

But no matter what i try i cant get it to work.. Is this still true that websocket servers dont work with Phusion Passenger in combination with Apache and Node.js in 2023? Or am i missing something else? Im trying to find out where the problem is but i am a bit lost of where to look.. Maybe someone else who tried the same that can help me out?

0

There are 0 best solutions below