7

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.

Benny Bottema
  • 170
  • 1
  • 1
  • 5
  • very interesting. And how are you going to provide sticky balancing when you need more than 1 primary server in your scheme? – Oleg Kuralenko Oct 20 '17 at 21:34

4 Answers4

4

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.

Morteza j8
  • 103
  • 4
Devitgg
  • 41
  • 2
1

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.

Jeff
  • 111
  • 3
1

This has been my NGINX configuration for over 6 years and it allows for multiple backup servers. However, I believe it needs to be on different ports. If the same port is used and busy then it can't properly back it up. I use PM2 to start my app on 5 ports and my nginx uses those app servers.

server 127.0.0.1:3000;             # NodeJS Server 1
server 127.0.0.1:3002;             # NodeJS Server 2
server 127.0.0.1:3003;             # NodeJS Server 3
server 127.0.0.1:3004 backup;      # NodeJS Server 4 - backup 1
server 127.0.0.1:3005 backup;      # NodeJS Server 5 - backup 2
Joel Noe
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 28 '23 at 13:23
0

I did a test and seems that nginx does not support multi-backup servers.

upstream a {
    server main_server:8080 max_fails=2 fail_timeout=5;
    server backup1:8080 backup;
    server backup2:8080 backup;
}

Whenever main_server:8080 is down, backup1:8080 replies properly. When main_server:8080 and backup1:8080 are down, the connection is refused and it never makes it to backup2:8080.

  • Nginx definitely allows multiple backup servers in their configurations, it's in their examples: http://nginx.org/en/docs/http/ngx_http_upstream_module.html. It's possible they require all backups to be available but that seems quite silly. I think one option would be to set another upstream for the backup and set a load balance and/or backup within *that* upstream. Inception! – duct_tape_coder Feb 25 '20 at 22:54
  • Is it possible to do that? Add an upstream to another upstream – JMSamudio Jul 27 '20 at 15:37