Networking

How to setup simple load balancing with NGINX

Jack Wallen shows how to configure basic load balancing on the NGINX web server, so your company can handle heavier web traffic.

Image: Jack Wallen

If your company depends upon the NGINX web server, you've probably been looking for a way to set up load balancing. If you're not sure of what exactly load balancing is, I'll leave this here:

Load balancing is a system that distributes network or application traffic across a number of servers.

For example, you have your primary NGINX web server and want to distribute the load across two or three other NGINX servers; by doing this, you can ensure that no matter how much traffic you're getting, more than one server can take care of the load. Clearly, your web sites (on each NGINX server) will have to be set up identically for this to work (otherwise, you'd be distributing traffic across differing sites).

With that said, I'm going to show you just how easy it is to setup NGINX for load balancing. I'll be demonstrating on Ubuntu Server 16.04, with three servers at IP addresses:

  • Server 1 - 192.168.1.232
  • Server 2 - 192.168.1.233
  • Server 3 - 192.168.1.234

You will alter the configuration to meet your specific IP address scheme and needs (say you want to distribute across more servers). I will assume you already have NGINX up and running on each server.

Let's get to work.

SEE: Hiring kit: MongoDB administrator (Tech Pro Research)

Initial setup

I'm going with absolute basics here. To demonstrate how this works, I will create a new index.html in each server's /var/www/html directory, with the content:

<h1>NGINX SERVER Y</h1>


Where Y is either 1, 2, 3 (depending on which server the file is on).

With that in place, let's configure NGINX.

SEE: 20 quick tips to make Linux networking easier (free PDF) (TechRepublic)

Configuration

We're going to create a new file to configure load balancing on SERVER 1 only. Issue the command sudo nano /etc/nginx/conf.d/load-balancer.conf. In that file, copy the following contents:

# Define which servers to include in the load balancing scheme.

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

# This server accepts all traffic to port 80 and passes it to the upstream.

server {
   listen 80;

   location / {
      proxy_pass http://backend;
   }
}


NOTE: Make sure to configure the above file with your IP address scheme. You can also add as many servers as necessary for load balancing.

Save and close that file.

On our Ubuntu system, we have to remove the symbolic link to default, in the /etc/nginx/sites-enabled folder. Do this with the command:

sudo rm /etc/nginx/sites-enabled/default


Restart NGINX with the command:

sudo systemctl restart nginx

Testing

If you point a browser to the IP address of Server 1 (in our case 192.168.1.232), the load balancing will begin to round-robin requests to the NGINX servers at the other addresses, configured within the load-balancer.conf file. Your first call to Server 1's IP address will take you to Server 2 and the next to Server 3 (and so on).

One caveat

The default load balancing system is round robin. This can cause problems with session persistence. If your web sites/applications require users to be directed to the same server used for their previous session, IP hashing must be used. With IP hashing in place, a user will always be directed to the same server (so long as the server is available).

To setup IP hashing, open up the /etc/nginx/conf.d/load-balancer.conf file and add the line ip_hash; below upstream backend {. That new file would look like:

# Define which servers to include in the load balancing scheme.

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

# This server accepts all traffic to port 80 and passes it to the upstream.

server {
   listen 80;

   location / {
      proxy_pass http://backend;
   }
}


Save and close that file. Restart NGINX with the command:

sudo systemctl restart nginx


Now if you point your browser to the Server 1 IP address and hit refresh, you will be sent to the same server.

Simple and effective

That's how easy it is to setup basic load balancing with NGINX. There are other tricks you can use with this system; but, at the moment, you now have multiple servers that can balance the load of your website or web application. Next time around, we'll talk about defining server weights and load balancing HTTPS.

Also see

About Jack Wallen

Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.

Editor's Picks

Free Newsletters, In your Inbox