'NGINX not loadbalancing for WS connections

I have two local backend applications running on :

  • 127.0.0.1:4444
  • 127.0.0.1:4445

Both of which support websocket connections (totally identical)

I have set an nginx on my local machine in order to loadbalance the ws requests coming in using round-robin:

nginx (listening on port 5555):


map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    ip_hash;
    server 127.0.0.1:4444; 👈
    server 127.0.0.1:4445; 👈
}

server {
        listen       5555; 👈
        server_name  localhost;

        location / {
            # root   html;
            # index  index.html index.htm;

            ##### WS SUPPORT BELOW #####
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }

The problem is, client (REACT application) makes connection with (via port 5555) ONLY with server1 (as per the logs).

I can't figure out why nginx is not distributing the requests to both server with round-robin algorithm. Am I missing something here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source