How to use Nginx as a Load Balancer Server


To give a quick introduction to the load balancer, make sure to read our EDU page first. We’ll be exploring the way of setting up the Nginx as a load balancer server.

Specification:

OS Version: CentOs 8.2 x64
Nginx Version: nginx/1.14.1

How to configure Nginx as a load balancer server

We are using the main Nginx configuration file for this task: /etc/nginx/nginx.conf. To create a balancing function we need two main configuration changes to be made:

  1. Create an upstream configuration block to define all the backend servers Nginx should balance between.
  2. Use proxy_pass directive (similar to creating a reverse proxy) to call the previously created upstream block.

Upstream Block

Nginx upstream configuration block is used to define the backend servers used to balance the load to. Also, upstream configuration block defines what are the conditions balancing is deployed.

Below is the example of the upstream configuration block we’ll use in our example. The following code is placed within the server context:

upstream lb_example {
                server 12.13.14.15 weight=1;
                server 13.14.15.16 weight=1;
                server backup.bluegrid.io backup;
        }

The above configuration basically says this:

  • Out of two consecutive requests, one should go to the server 12.13.14.15 and the other request to the server 13.14.15.16.
  • If none of the two backend servers are accessible use backup.bluegrid.io as a backup backend server.

Note: if the weight parameter was set like this:

server 12.13.14.15 weight=3;
server 13.14.15.16 weight=1;

It would mean that out of four consecutive requests, three will go to the server 12.13.14.15 and one to the server 13.14.15.16.

proxy_pass directive

In its nature, the Nginx load balancer is actually a form of the reverse proxy server. It will behave exactly like a reverse proxy with only one difference: it can “round-robin” between multiple backend servers. To use the effect of the upstream block we should call it from within a location block:

location / {
                proxy_pass http://lb_example;
        }

Practically, that would be it. If we want to add a caching layer on top of this configuration we can merge the knowledge from using Nginx as a reverse proxy and this article:

server {
	listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        proxy_cache_key $scheme$http_host$uri$is_args$args;

        # security for bypass so localhost can empty cache
        set $bypass $http_cache_purge;

        location / {
                proxy_cache the_zone;
                proxy_pass http://lb_example;
                proxy_cache_valid 200 1d;
                add_header Cache $upstream_cache_status;
                proxy_cache_min_uses 2;
                proxy_cache_bypass $bypass;
        }
	      upstream lb_example {
                server 12.13.14.15 weight=1;
                server 13.14.15.16 weight=1;
                server backup.bluegrid.io backup;
        }
}

With the above configuration, we have a reverse proxy/load balancer with a caching mechanism! For deeper dive make sure to read the Nginx documentation on this topic.

Talk to our Nginx experts
Share this post

Share this link via

Or copy link