How to Setup Load Balancer in NGINX


setup load balancer in nginx

NGINX can act as load balancer across multiple back-end servers, thereby reducing server loads and improving website performance. Here’s how to setup load balancer in NGINX.

 

How to Setup Load Balancer in NGINX

Here are the steps to setup load balancer in NGINX. We’ll setup NGINX for load balancing on Ubuntu server, across 3 IP addresses.

Server 1 – 192.168.1.23
Server 2 – 192.168.1.24
Server 3 – 192.168.1.25

Before you proceed, please ensure you have installed NGINX on your system

 

1. Create files for testing

We will create 3 index.html files, each with slightly different content and place them in the /var/html/www folders of each of the 3 backend servers.

For example, you can create index.html file with the following content


<h1>NGINX SERVER Y</h1>

where Y – 1, 2, 3 depending on which backend server you place this file on.

 

2. Configure NGINX

Next we configure NGINX to send the incoming traffic to one of the 3 IP addresses. For our example, we will set up load balancer in Server 1. Open terminal in Server 1 and run the following command to create a load balancer config file.


$ sudo nano /etc/nginx/conf.d/load-balancer.conf

 

Add the following code:


# List of servers to include in the load balancing scheme.

upstream backend {
 server 192.168.1.23;
 server 192.168.1.24;
 server 192.168.1.25;
}

# Accepts all traffic to port 80 and passes it to the upstream.

server {
 listen 80;

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

Save and close the file

 

Basically, it tells NGINX on Server 1 to listen to port 80 and round-robin requests among the 3 IP addresses mentioned in “upstream” block. You can add more IPs here if you want.

 

Remove symbolic link to default in /nginx/sites-enabled folder


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

 

Restart NGINX to apply changes


$ sudo systemctl restart nginx

 

4. Test Load Balancing in NGINX

If you point to your Server 1 (192.168.1.23) it will round-robin across the 3 back-end servers to process your requests and send you the response. So you’ll see “NGINX SERVER 1” in your response, then “NGINX SERVER 2” the next time and so on. This indicates that your NGINX is load balancing across 3 IP addresses.

 

5. Session Management (Optional)

In case of above configuration, since the requests are sent to different server every time, it can cause issues for session persistence. If you want your users to be sent to the same server as their previous session, you need to use IP hashing.

To do that, open load_balancer.conf and add the ip_hash line as shown, inside “upstream” block


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

 

Save and close the file. Restart NGINX to apply changes


$ sudo systemctl restart nginx

 

Now if you point to server 1, your request will be sent to the same back-end server everytime.

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!