Score:0

Nginx: Websocket on port 80 is not working

ar flag

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.

djdomi avatar
za flag
Does this answer your question? [How does nginx websocket proxy work?](https://serverfault.com/questions/723121/how-does-nginx-websocket-proxy-work)
Bidyut avatar
ar flag
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.
djdomi avatar
za flag
is your question solved?
Bidyut avatar
ar flag
yes, we were able to fix the issue, by using the solution commented by me.
djdomi avatar
za flag
please add the answer by yourself and accept it 24h later
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.