Nginx round-robin nor exactly round-robin

load balancingnginxreverse-proxy

I have a very basic round-robin reverse proxy setup for learning purposes.

upstream file_server_com {
    server machine-01;
    server machine-02;
}

server {
        listen 80 default_server reuseport;
        listen [::]:80 default_server ipv6only=on reuseport;

        root /usr/share/nginx/html;
        index index.html index.htm;

        server_name localhost;

        location / {
            proxy_pass http://file_server_com;
        }
}

I have a 1kb file on the each of the upstream servers. I run wget to download the file. I'm not seeing the requests ping-pong between machine 01 and 02. Overall, I do see about a 50/50 split between the two, but I would have thought I should see the requests alternate between the two machines.

Sometimes I'll see machine 01 get the request consecutively, sometimes I'll see machine 02 get consecutive requests, sometimes they alternate.

Is there something I'm not understanding about nginx's round-robin load balancer?

Best Answer

You could try to force nginx to consider all servers having the same weight:

upstream file_server_com {
    server machine-01 weight=1;
    server machine-02 weight=1;
}

that might help instill the right algorithm.

Or use the least_conn attribute if round-robin cannot get obtained. At least, the least busy server should be selected.

But according to this SO answer nginx seems to deal differently with round-robin when multiple threads are running. The "solution" was to set

worker_processes 1;

but that is likely to reduce performance.