Skip to content

mpolinowski/ws-api-proxy

Repository files navigation

Using the NGINX as a Proxy for Websocket APIs

Taking a peek at proxy'ing websocket services. This repo heavily borrows from:

See also:

Backend

Start the backend Node.js service by running:

npm install
node server1.js

For the load-balancing example add the additional servers:

node server2.js
node server3.js

Proxy

As proxy I am using an NGINX container that can be started with the configuration files provided in docker_ws_proxy:

docker run --rm --network host -v /path/to/docker_ws_proxy:/etc/nginx --name proxy nginx:alpine

As server address I always used localhost instead of an IP or domain - you might want to change that:

Which configuration file is used by NGINX can be set in the following place:

http {
    # make sure to cut http connection after ws upgrade
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    # Simple websocket proxy
    include /etc/nginx/conf.d/ws-proxy.conf;
    # Simple secure websocket proxy
    # include /etc/nginx/conf.d/wss-proxy.conf;
    # Secure websocket proxy with path-rewriting
    # include /etc/nginx/conf.d/wss-proxy-path-rewriting.conf;
    # Secure websocket proxy with load-balancing
    # include /etc/nginx/conf.d/wss-proxy-load-balancing.conf;
}