0

I have a simple NodeJs Websocket application running, the code for it is

// Importing the required modules
const WebSocketServer = require('ws');

// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: 8090 })

// Creating connection using websocket
wss.on("connection", ws => {
    console.log("new client connected");
    // sending message
    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 8090");

I want to proxy the requests coming to port 80 and redirect it to the 8090. I am using Nginx for reverse Proxy, with configuration

server {
  listen 172.30.5.139:80;
  listen 172.30.5.139:443;

  location / {
    proxy_pass "http://127.0.0.1:8090/";
    proxy_http_version  1.1;
    proxy_set_header    Upgrade $http_upgrade;
    proxy_set_header    Connection "upgrade";
    proxy_set_header    Host $http_host;
  }
}

While testing I can connect via

 ws://1.2.3.4:443/

But cannot connect via

ws://1.2.3.4/

In Nginx access log I am seeing the following error:

51.24.28.78 - - [06/Oct/2022:14:19:42 +0000] "GET /ws HTTP/1.1" 426 16 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 " "-"

Need help in figuring out the underlying issue, as I am planning to support both ws and wss by implementing SSL on Nginx.

Bidyut
  • 121
  • 3
  • Does this answer your question? [How does nginx websocket proxy work?](https://serverfault.com/questions/723121/how-does-nginx-websocket-proxy-work) – djdomi Oct 06 '22 at 17:39
  • What I figured out is that, on port 80, the request are being treated as HTTP request and cannot be differentiated by Nginx, but using port other than 80 resolves this issue. For fixing it, I am making Nginx listen on port 443. – Bidyut Oct 07 '22 at 04:26
  • is your question solved? – djdomi Oct 08 '22 at 16:27
  • yes, we were able to fix the issue, by using the solution commented by me. – Bidyut Oct 10 '22 at 12:13
  • please add the answer by yourself and accept it 24h later – djdomi Oct 16 '22 at 17:22

0 Answers0