How Do You Set up Nginx to Be a Reverse Proxy That Conveys Inbound Connections to the Web Server with the Fewest Active Connections?

Problem scenario
You want your website to perform well.  By default Nginx's load balancing system uses the round-robin technique.  You want available yet under-utilized web servers to handle inbound connection requests (aka someone using a web browser to go to a web page).  Individual users can put a disparate load on a given Nginx instance.  Some website users will spend a significant amount of time on a web page.  Others will invoke various application features that will require more resources than others.  Ultimately you want to be sure that  servers with fewer active connections will receive more traffic to balance the load.  Like a router or networking hardware device, a physical server with a NIC running Nginx can relay traffic to specific servers to balance loads and enable fault-tolerance.  How do you avoid the simple round-robin distribution technique and configure Nginx to balance the HTTP traffic load by relaying it to the available web server with the fewest active connections?

You need to have Nginx installed (for CentOS you can click here or for Debian/Ubuntu click here) or you must have configured Nginx as a an HTTP load balancer (or reverse proxy server).  To do the latter, see this article which will actually work for Nginx distributing traffic to regular Nginx websites, Apache websites, or Nginx websites running in Docker containers.  This solution below assumes that no web traffic can bypass the Nginx HTTP load balancer. In some environments, using a special DNS name or port in a web browser can bypass the load balancer.

1.  To use Nginx as a reverse proxy or load balancer with the guideline that traffic goes to the web server with the fewest active connections, you need one Nginx web server to be a distributor.  This means modifying the /etc/nginx/conf.d/default.conf file (or /etc/nginx/conf.d/nginx.conf) in two sections or what Nginx people refer to as "blocks."  (If you want to know the difference between these two files, see this posting.) The changes that need to take place are these:

a) Comment out in the server {} section these lines under the "location / {" section (but there may only be one of these stanzas that you find):

# root /usr/share/nginx/html;
# index index.html index.htm;

b)  Do not comment out the "location / {" stanza itself.  Do add a line under this stanza near the two commented-out lines above with this as the content indented the same way as the original lines above:

proxy_pass http://backend;

c) At the very bottom of the file, add these lines (but replace the IP addresses with web servers, e.g., regular Nginx instances):

upstream backend {

2.  The Nginx services then need to be brought down and brought back up (cycled).

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

The default web page page for Nginx installations can be located on a Linux server /var/www/html/index.nginx-debian.html

Leave a comment

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