How to Setup Nginx Load Balancing on Ubuntu Server

What Is Nginx Load Balancing?

Nginx Load balancing is a free, open source and one of the most popular software used for web servers, caching, reverse proxying, mail proxy and load balancing. Nginx is capable of handling thousands of concurrent connections. However, when your website user’s and traffic are growing fast then you will need to implement a load balancer in front of your website.

Load Balancing is used when you want to scale your application and increment its performance and redundancy. Nginx is the most sturdy and popular load balancer that works at improving the availability and efficiency of the server. Nginx acts as a single entry point to a distributed web application working on multiple separate servers.

setup nginx load balancing

Nginx Load balancing allows you to add or remove servers as demand dictates. Nginx load balancer distributes incoming network traffic and workload to separate groups of application servers. In each of these cases, it returns the response from the selected server to the accurate client.

Nginx Load Balancing Methods

  1. Round robin: In this method, the request is distributed in a roundrobin way. When no method is specified for distribution, it is used automatically.
  2. Ip hash: It is a method in which hash functions are used to identify the server to be used for the next request based on the IP address of the clients. With the help of this method, you can opt for session persistence.
  3. Least connected: Using this method, you can assign the upcoming request to the server that is not so busy, i.e., the server with few active connections.

Nginx Load Balancer Environment Setup

Nginx load balancer setup

In this post, we will explain how to set up Nginx as a Load Balancer on the Ubuntu server.

Install Nginx on Load Balancer Host

First, you will need to install the Nginx server on the Load Balancer host. You can install it by running the following command:

				
					apt-get install nginx -y
				
			

Once the Nginx has been installed, start the Nginx service and enable it to start at system reboot:

				
					systemctl start nginx 
systemctl enable nginx 
				
			

Now, you can verify the status of the Nginx service using the following command:

				
					systemctl status nginx
				
			

If everything is fine, you will get the following output:

				
					● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2021-10-11 19:22:31 UTC; 26s ago
       Docs: man:nginx(8)
    Process: 62622 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 62623 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 62716 (nginx)
      Tasks: 2 (limit: 1041)
     Memory: 3.9M
     CGroup: /system.slice/nginx.service
             ├─62716 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─62719 nginx: worker process

Oct 11 19:22:31 ubuntu systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 11 19:22:31 ubuntu systemd[1]: Started A high performance web server and a reverse proxy server.
				
			

By default, Nginx web server listens on port 80. You can check it by running the following command:

				
					ss -antpl 
				
			

You should see the Nginx port 80 in the following output:

				
					State     Recv-Q    Send-Q       Local Address:Port         Peer Address:Port    Process                                                       
LISTEN    0         511                0.0.0.0:80                0.0.0.0:*        users:(("nginx",pid=62719,fd=6),("nginx",pid=62716,fd=6))    
LISTEN    0         4096         127.0.0.53%lo:53                0.0.0.0:*        users:(("systemd-resolve",pid=62850,fd=13))                  
LISTEN    0         128                0.0.0.0:22                0.0.0.0:*        users:(("sshd",pid=62835,fd=3))                              
LISTEN    0         511                   [::]:80                   [::]:*        users:(("nginx",pid=62719,fd=7),("nginx",pid=62716,fd=7))    
LISTEN    0         128                   [::]:22                   [::]:*        users:(("sshd",pid=62835,fd=4))                              

				
			

Now, open your web browser and verify the Nginx test page using the URL http://192.168.0.10. You should see the Nginx test page in the following screen:

nginx web server

At this point, your Nginx web server is installed and running. You can now proceed to the next step.

Configure Nginx as a Load Balancer

Next, you will need to create an Nginx virtual host configuration file for the load balancing setup. You can set up the Nginx load balancing using the three methods.

1. Set Up Nginx Using Round robin Method

You will need to create an Nginx virtual host configuration file to implement the load balancer. You can create it with the following command:

				
					nano /etc/nginx/conf.d/loadbalancer.conf
				
			

Add the following configuration for round robin method:

				
					upstream backend {
        server 192.168.0.11;
        server 192.168.0.12;
    }
	
    server {
        listen      80;
        server_name 192.168.0.10;

        location / {
	        proxy_redirect      off;
	        proxy_set_header    X-Real-IP $remote_addr;
	        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header    Host $http_host;
		proxy_pass http://backend;
	}
}
				
			

Where: 192.168.0.10 is the IP address of load balancer, 192.168.0.11 is the IP address of the first backend server and 192.168.0.12 is the IP address of the second backend server.

Save and close the file when you are finished then restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

In this method, the request is distributed in a round robin way.

2. Setup Nginx Using IP Hash Method

In this section, we will set up Nginx load balancing using the Ip hash method. This method uses an algorithm that takes the source and destination IP addresses of the client and server to generate a unique hash key. This key is used to allocate the client to a particular server.

First, create an Nginx virtual host configuration file with the following command:

				
					nano /etc/nginx/conf.d/loadbalancer.conf
				
			

Add the following configuration:

				
					upstream backend {
	ip_hash;
        server 192.168.0.11;
        server 192.168.0.12;
    }
	
    server {
        listen      80;
        server_name 192.168.0.10;

        location / {
	        proxy_redirect      off;
	        proxy_set_header    X-Real-IP $remote_addr;
	        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header    Host $http_host;
		proxy_pass http://backend;
	}
}
				
			

Save and close the file when you are finished then restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

3. Set Up Nginx Using Least Connected Method

In this section, we will set up Nginx load balancing using the Least Connected method. This method directs the requests to the server with the least active connections at that time. First, create an Nginx virtual host configuration file with the following command:

				
					nano /etc/nginx/conf.d/loadbalancer.conf
				
			

Add the following configuration:

				
					upstream backend {
        least_conn;
        server 192.168.0.11;
        server 192.168.0.12;
    }
	
    server {
        listen      80;
        server_name 192.168.0.10;

        location / {
	        proxy_redirect      off;
	        proxy_set_header    X-Real-IP $remote_addr;
	        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_set_header    Host $http_host;
		proxy_pass http://backend;
	}
}

				
			

Save and close the file when you are finished then restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

Test Nginx Web Servers

After setting up Nginx load balancing using one of the three methods. You will need to check whether the Nginx load balancing works or not.

To test it, open your web browser and access your application server using the URL http://192.168.0.10. You should see your first application server’s page:

test nginx load balancer

Now, refresh the page continuously. After some time, the application should be loaded from the second server and you should see your second application server’s page:

test nginx load balancing

Setting up Nginx Completed

In the above guide, we explained how to set up Nginx load balancing using three methods. You can now use your prefered method set up an Nginx load balancing, and scale out your application and increase its performance and redundancy. For more information, visit the Nginx load balancing documentation.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x