Create multiple websocket server with one port number

netty, websocket

Solution

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

- Nginx listening at `0.0.0.0:1234`

- Netty that serves `/PathA` and listens at `0.0.0.0:1235`

- Netty that serves `/PathB` and listens at `0.0.0.0:1236`

- 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";
    }
}

Problem

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?

Original source