Nginx – Does order of servers in nginx config matter

configurationhigh-availabilitynginx

I have a custom tool which generates a part of nginx config in the following form, for example:

upstream  backend  {
  ip_hash;
  server server1:8080;
  server server2:8080;
  server localhost:8080 backup;
 }

From time to time some servers go down, some go up and the next update could produce following:

upstream  backend  {
  ip_hash;
  server server1:8080;
  server server3:8080;
  server server2:8080 down;
  server localhost:8080 backup;
 }

As you can see, server3 has appeared and server2 is marked down. My question is following: does nginx care the order of servers presented in config? Do I need to sort output of my custom tool?

In this example the initial order was changed and I don't know if nginx will handle it correctly.

Best Answer

IIRC, w/ the ip_hash rotation method, order does matter. Nginx hashes the client ip, then sends clients to one of the items in the list. If you change the order, ip_hash will direct clients to another upstream node. Per the recommendation here, I'd leave the entire list of servers in your file all the time, and simply mark it 'down' if it's offline.