How do multiple backup servers work in Nginx?
For the backup
directive, The Nginx documentation states rather minimally:
marks the server as a backup server. It will be passed requests when the primary servers are unavailable.
What if you have multiple backups and the primary server goes down, is one of the backups appointed the new primary? Or will Nginx Round Robin between them?
Context:
I have a primary server and multiple backups, but all connections should always go to the same primary or backup. Sort of like the ip_hash load balancing mode except it should use the same server for all connections and clients.
While it does not support multi-backup servers in a context as @Alberto Mendoza answered --
If you place the backups to another VPS running a NGINX
load balancer to its own backups... this is a work around I am currently using for a multi-regional network.
upstream routing {
server main_server:8080 max_fails=2 fail_timeout=5;
server backupServer1:8080 max_fails=1 fail_timeout=5;
server backupServer2:8080 backup;
}
If your main server is unresponsive for 2 fails -- it will attempt your first VPS backup -- if for some odd reason that is being DDOS or for whatever reason is down also -- it will go to your third VPS .. You can continue to daisy chain as needed.
Well, recently I also did the test... just like @alberto-mendoza:
upstream a {
least_conn;
server main_server:8080 max_fails=2 fail_timeout=5;
server backup1:8080 backup max_fails=1 fail_timeout=30;
server backup2:8080 backup max_fails=1 fail_timeout=60;
}
And nginx did the right load balance betwen the backup servers when the main goes down (using the same balancing method chosen for the main servers).
It's also accept parameters like max_fails and fail_timeout for backup servers.