Dany Joumaa
Dany Joumaa

Reputation: 2040

How to configure nginx to forward websockets to a different process

I have a REST API served by a node express server at port 3000. Using socket.io, there is also a websocket attached to the express server on that same port.

In order to scale our node server, we've decided to split off the websocket server into an entirely new process, and have it hosted on a new port (3002)

How do I configure nginx to forward all websocket messages to the new process on port 3002, while forwarding everything else to the original REST API server on port 3000?

Upvotes: 3

Views: 2999

Answers (1)

Renjith Thankachan
Renjith Thankachan

Reputation: 4336

You just configure a location directive for proxying you websocket server

location /websocket/ {

    proxy_pass http://127.0.0.1:3002;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

If your connection client is pure websocket client, then you may need to set websocket connection url to ws://example.com/websocket/socket.io/?EIO=3&transport=websocket

Upvotes: 6

Related Questions