How Do You Set up Nginx as an HTTP Load Balancer So Client Requests (from Web Browsers) Do Not Go to Certain Nginx Servers unless Others Are Down?

Problem scenario
You have a web server running Nginx that acts as a reverse proxy server.  On occasion your regular web (Nginx) servers go down.  You want to have one or two web (Nginx) servers that are  reserved as backups exclusively.  You do not want traffic going to these servers unless the main Nginx servers are unavailable (either due to network or server failure).  You can allocate RAM and CPU to these reserved servers on demand.  To save money the business wants these servers with few resources unless they are brought online.  How do you have Nginx convey traffic to other Nginx  servers only when the main Nginx servers are down?

Solution
Prerequisite

You must have configured Nginx as a an HTTP load balancer (or reverse proxy server).  To do this, see this article which will actually work for Nginx distributing traffic to regular Nginx websites, Apache websites, or Nginx  websites running in Docker containers. 

Procedures
In the default.conf file, use the "backup" directive near the servers' FQDNs or IP addresses as they appear in the "upstream backend" clause (within the braces of "upstream backend {}").

This "backup" designation is performed with Nginx in Docker containers the same way it is done with Nginx running directly on a server.  Arguably one difference would be that the IP address you use in the default.conf file is an internal IP address associated with the user defined Docker network.

To find the IP address that would be ideal when you know the container ID of the Nginx instance you want to be a back up, you could use this command:

docker inspect <containerID> | grep IPAddress | tail -n 1

That would give you the internal IP address of the Docker container of your Nginx instance.

The Nginx instance with the landing page should have a default.conf file (in /etc/nginx/conf.d/default.conf).  Here is an example of two Nginx servers reserved as "backup" in the default.conf file:

upstream backend {
  server 10.10.10.10;
  server 10.10.10.11;
  server 10.10.10.12 backup;
  server 10.10.10.13 backup;
}

These "backup" keywords do take take effect until you stop Nginx services and restart them.  These keywords work with Nginx running in Docker no differently from Nginx running directly on a server.

FFR
If you want to need to troubleshoot problems with your Nginx load balancer, see this link.

Leave a comment

Your email address will not be published. Required fields are marked *