0

I'm trying to set up a websocket server using node.js on an existing apache server. Since the server is SSL I need a websocket to also be secured. For that I'm trying to implement the proxy solution. I'm redirecting any websocket request via vhost this way:

<VirtualHost 127.0.0.1:80>
  ServerName status.localhost
  <Location /server-status>
    Require local
    SetHandler server-status
  </Location>
  
    RewriteEngine On
  
    # When Upgrade:websocket header is present, redirect to ws
    # Using NC flag (case-insensitive) as some browsers will pass Websocket
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)    ws://localhost:8888/$1 [P,L]
    
    # All other requests go to http
    ProxyPass "/" "http://localhost:443/"
    
</VirtualHost>

Checked that mod_proxy_wstunnel.so and mod_proxy.so are loaded in httpd.conf.

Then I took a simple nodejs websocket implementation sample and ran it

// Importing the required modules
const WebSocketServer = require('ws');
 
// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: 8888 })
 
// Creating connection using websocket
wss.on("connection", ws => {
    console.log("new client connected");
 
    // sending message to client
    ws.send('Welcome, you are connected!');
 
    //on message from client
    ws.on("message", data => {
        console.log(`Client has sent us: ${data}`)
    });
 
    // handling what to do when clients disconnects from server
    ws.on("close", () => {
        console.log("the client has connected");
    });
    // handling client connection error
    ws.onerror = function () {
        console.log("Some Error occurred")
    }
});
console.log("The WebSocket server is running on port 8888");

And try to connect using this client:

const WebSocket = require('ws');
s = new WebSocket("wss://hw-assist.org:443");
s.onopen = function() {console.log("OPENED")};

However when trying to connect I'm getting the following:

Error: Unexpected server response: 301
    at ClientRequest.<anonymous> (/home/bitnami/jstest/node_modules/ws/lib/websocket.js:888:7)
    at ClientRequest.emit (events.js:314:20)
    at HTTPParser.parserOnIncomingClient (_http_client.js:601:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:122:17)
    at TLSSocket.socketOnData (_http_client.js:474:22)
    at TLSSocket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
    at readableAddChunk (_stream_readable.js:272:9)
    at TLSSocket.Readable.push (_stream_readable.js:213:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on WebSocket instance at:
    at emitErrorAndClose (/home/bitnami/jstest/node_modules/ws/lib/websocket.js:1004:13)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

What am I missing? Thanks!

user1617735
  • 451
  • 5
  • 16

0 Answers0