Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple websocket server with one port number

I am using netty 4.0.20 I want to create different websocket servers on the same port using different urls

for example, wss://localhost:1234/PathA

wss://localhost:1234/PathB

wss://localhost:1234/PathC

is that possible?

like image 818
Manos Avatar asked Mar 09 '26 09:03

Manos


1 Answers

Yes, this is possible with using reverse proxying, which can be done with Nginx.

This will require one additional server in your setup.

First you have to setup each server to listen to a different port and then you need the front end server to listen to your desired public port (in your case, this is 1234).

So lets say you have the following servers

  1. Nginx listening at 0.0.0.0:1234
  2. Netty that serves /PathA and listens at 0.0.0.0:1235
  3. Netty that serves /PathB and listens at 0.0.0.0:1236
  4. Netty that serves /PathC and listens at 0.0.0.0:1237

Now what you have to do is write an Nginx configuration file that will upgrade the connection from HTTP to Websocket and then reverse proxy each path to its corresponding server. An example configuration file that could do the job for you is the following.

{
    listen 1234;
    server_name localhost;

    location ~PathA/$ {
        proxy_pass http://localhost:1235;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }

    location ~PathB/$ {
        proxy_pass http://localhost:1236;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }

    location ~PathC/$ {
        proxy_pass http://localhost:1237;
        proxy_http_version 1.1;
        proxy_set_header Upgrade "websocket";
        proxy_set_header Connection "upgrade";
    }
}
like image 92
Paris Avatar answered Mar 11 '26 09:03

Paris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!