weight and balancing in upstream proxy

Francis Daly francis at daoine.org
Thu Apr 13 23:49:07 UTC 2017

On Wed, Apr 12, 2017 at 02:50:08PM -0700, Frank Liu wrote:

Hi there,

> How does nginx balances traffic to upstream with different weight? If I
> have 3 servers in upstream, with weight 1, 2, 4, assuming all are healthy,
> will nginx send traffic to server 1, 2, 3, 2, 3, 3, 3 or 1, 2, 2, 3, 3, 3,
> 3?

If you want to know what your current nginx version does, it should not
be too difficult to test:

One nginx.conf. One http section. One upstream{} listing multiple
ip:ports. One server{} which proxy_pass:es to that upstream. Multiple
server{}s, each of which listens on one ip:port, writes to a different
access_log, and does something like "return 200 ok;".

Then for increasing numbers X, "GET /X" on the main server. Look at
the individual access log files to see which server handled /1, which
handled /2, etc. By the time you get to 700, you'll either see that
there is a reliably repeating pattern, or you'll see that it is probably

If you actually care about what pattern is used; or if you want to
guarantee that the same pattern will be used in future nginx versions;
then get your preferred code written and use that instead of whatever
nginx uses.

If you want to know what guarantee there is that the behaviour will not
change in the future: I'd say "none", except that there is a good chance
that what is written in the documentation will be honoured. Paraphrasing
that, for the above case: for 7 requests, 1 will go to the first server;
2 to the second; and 4 to the third.

> If I have two servers with both weight 50, will nginx will 50 requests
> to server 1, and then 50 to server 2, or will it calculate the ration to be
> 1:1 and send one after another?

Same answer: it does not seem to difficult to test, if you don't want
to read the available source.

Good luck with it!

Francis Daly        francis at daoine.org

More information about the nginx mailing list