How Do You Set up Nginx as an HTTP Load Balancer So Client Requests (from Web Browsers) Go to Certain Nginx Servers More Frequently Than Others?

Problem scenario
You have certain Nginx servers with ample resources whereas others have minimal resources.  Based on geographic locations and data center bandwidth locations and costs, you want to assign fractions of the web traffic from client workstations (requests from web browsers) to different Nginx servers more than others.  You do not want round-robin, equal distribution of traffic.  You want customized HTTP load balancing in accord with unequal configurations.  How do you distribute this traffic proportionately according to your desired specifications?


You must have configured Nginx as 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. 

Use the "weight" keyword in the default.conf file.  Even regular Docker containers on Linux severs will still support the "find" keyword.  Therefore you can always use "find / -name default.conf" to find the Nginx configuration file.  This file should have an "upstream backend {}" section.  Here is an example:

upstream backend {
  server weight=5;
  server weight=4;
  server weight=2;
  server weight=4;

The higher the weight, the greater the traffic that is sent to the server.  In this instance the server would receive half the traffic that server would receive.  This weight directive works the same for Nginx instances running directly on a server or running in a Docker container.  The default weight is 1 (according to DigitalOcean's website).  The percentage of traffic a server will get will be its individual weight divided by the sum of all the weights of the servers in the "upstream backend {}" section.

The "weight" values with Nginx in Docker behave no differently from Ngnix running on a server directly. 

Leave a comment

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