google-chromenginxvirtualboxreverse-proxy

Nginx Round Robin Load balancing Upstream Servers are not in sequential order, behaviour changes between chrome,firefox,IE browsers


Configured Nginx Server in a Virtual box(ubuntu server) and opened up the port 80 to windows host, Did the same to 5 Ubuntu servers with a basic node app that prints 1,2,3,4,5 with 5 servers respectively. Configured the Nginx.conf file

events{
}

http{
        upstream serverslist {
#               Server 8001
                server 192.168.100.2:3000;
#               Server 8002
                server 192.168.100.3:3000;
#               Server 8003
                server 192.168.100.4:3000;
#               Server 8004
                server 192.168.100.5:3000;
#               Server 8005
                server 192.168.100.6:3000;
        }
        server {
                location / {
                        proxy_pass      "http://serverslist/";
                }
        }
}

Ping and Curl to these servers individually from the Nginx server works perfectly and returns the expected result. I.e on each curl localhost gives 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. In this order. which is expected.

But when I load the Nginx server in Chrome Browser I get the output 1, 3, 5, 2, 4, 1, 3, 5, 2, 4.
And When I load the Nginx server in Firefox I get the output 1, 2, 3, 4, 5. (Correct)
When I load the Nginx IP in the duckduckgo browser on android it gives 5, 4, 3, 2, 1, 5, 4, 3, 2, 1.

I tried adding many proxy-based headers and just plain headers from the net, couldn't find any solution to get the same output in all browsers i.e 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. On Each reload of the same server.

Any help would be great.


Solution

  • Edit 3: Found out an interesting issue. In chrome every time I access my website it makes two calls no matter what they are called to/of my website and /favicon.ico of my website. I don't have a /favicon.ico. What I think is happening

    1. when Nginx is getting requests for/of my website, it is loading the first server upstream.
    2. when chrome loads / from my website it also calls /favicon.ico of my website which results in making a new call to Nginx so it loads the .ico files from the next server upstream.
    3. this happens so that servers 1,2,3 are loaded in order 1(ico file from 2),3(ico file from 1),2(ico file from 3). and cycle repeats.

    once I stopped the loading of /favicon.ico in Nginx, my three upstreams servers 1,2,3 are loading in order 1,2,3 of round-robin.

    I put this in the server with upstream to disable loading favicon.ico from Nginx.

    location = /favicon.ico {
      log_not_found off;
    }
    

    Hope anyone having this problem find this useful.

    Edit 2: Figured out the issue, the load balancing is working fine with static files and static servers inside the Nginx conf file. but my applications are being loaded by node, so had to start Nginx after starting all the node servers. Issue reappears when I restart the application server while Nginx is running. Now no issue will update soon

    Edit 1: This is not working for me anymore, this worked yesterday, today continued working on the same configuration, the issue reappeared.

    Had this same issue with my setup. What worked for me after a lot of proxy setup and VirtualBox setup and network editing.

    Add an extra server block in the HTTP block.

    server{
    }
    

    and reload the Nginx service.

    It worked for me, after reloading once both chrome and firefox loads the servers in the given order, I deleted the server block and it is still working.

    Don't know why the issue raised in the first place. Hope this helps to solve your issue.