Networking Open Source

How to set up server weight and HTTPS load balancing with NGINX

If you've configured NGINX for load balancing with a collection of servers that aren't quite equal, you can easily decide which of those servers will get the most traffic. Here's how.

Video: Stay on top of your network traffic with the help of these five simple apps

NGINX is a high performance web server that allows for just enough flexibility to help you set up your web server aspect of your data center in exactly the fashion you need. Along with this flexibility is one of the easiest means of gaining load balancing. Built into the NGINX system is everything you need to make sure your web servers are perfectly capable of standing up to growing traffic, without blinking a figurative eye.

If you already have basic load balancing setup, you might not know there are a few configuration options you can use to better suit your hardware. Say, for example, you have three servers running NGINX and are setup for load balancing. However, in that triad of hardware, you might have one server that's more powerful than the other two. Instead of having NGINX treat each of those servers as equals, it'd be nice to instruct the load balancer that Web Server 1 should get preference, Web Server 2 should fall next in line, and Web Server 3 should be the last in the chain of preference.

NGINX has that very capability, right out of the box. In fact, setting up server priority, with NGINX, is as easy as setting up load balancing. It's called "server weight" and I'm going to show you just how simple this feature is to configure.

I will assume you have NGINX installed and have read the piece How to setup simple load balancing with NGINX. With those two bits out of the way, let's add server weight into the mix.

SEE: Network security policy (Tech Pro Research)

The setup

We'll be working with the following servers:

  • Web Server 1 at IP 192.168.1.232
  • Web Server 2 at IP 192.168.1.233
  • Web Server 3 at IP 192.168.1.234

Our primary server is 1, secondary is 2, and tertiary is 3. We'll be adding weight to those servers in order to reflect that order.

Adding server weight

Open up the file /etc/nginx/conf.d/load-balancer.conf with the command sudo nano /etc/nginx/conf.d/load-balancer.conf. Since you've already setup load balancing with these servers, they should already be listed. What we have to then do is add the necessary weight to each. Server weight is added with the weight= option. For our purpose stated above, we would then change the server listing section from:

upstream backend {
   ip_hash;
   server 192.168.1.232;
   server 192.168.1.233;
   server 192.168.1.234;
}

To something like:

upstream backend {
   ip_hash;
   server 192.168.1.232 weight=4; 
   server 192.168.1.233 weight=3;
   server 192.168.1.234 weight=1;
}

You can omit weight=1, as 1 is the default value. I include it here for example sake. The server with the highest weight, will receive the majority of traffic. In the above setup, server 192.168.1.232 will receive traffic in a 4:1 ratio to server 192.168.1.234 and server 192.168.1.233 will receive traffic in a 3:1 ratio with server 192.168.1.234. If you're using server 192.168.1.232 as the load balancer, you can weight it as high as you want and it will not receive traffic. Of course, if server 192.168.1.232 is your load balancer, chances are you're not including in the above configuration. The master in this scenario is only used to decide where to direct traffic, so make sure you have chosen your server weights accordingly.

NOTE: You can use server weight alongside the ip_has option.

Once you've configured your weights, restart NGINX with the command:

sudo systemctl restart nginx

SEE: Server deployment/migration checklist (TechRepublic)

Your ideal weight

I highly recommend you run tests, in order to find out which server weight scheme best suits your needs. Eventually you'll find that perfect combination of weights that offers the ideal load balancing scheme. It shouldn't take much time to figure this ideal weight scheme out.

Also see

Image: Jack Wallen
Visit TechRepublic