Load balancing is a technique or a mechanism that distributes incoming requests to the group of backend servers. It's used to increase application availability, reliability and scalability. You can add many servers when traffic increases.
Nginx supports several methods for load balancing. A brief explanation of each are shown below:
- IP-hash : This method uses an algorithm that takes the source and destination IP address of the client and server to generate a unique hash key. IT allows for session persistence.
- Round-robin : It's the default method for load balancing. It instructs the load balancer to go back to the top of the list and repeats again.
- Least-connected : This method uses a dynamic load balancing algorithm. It distributes connections to the pool member that is currently managing the fewest open connections at the time the new connection request is received.
In this tutorial, we'll learn how to set up an Nginx load balancing on Ubuntu 20.04 server.
Let's get started!
Requirements
- Three servers running on Ubuntu 20.04.
- A valid domain name pointed to the load balancer server IP.
- A root password set up on each server.
For the purpose of this article, we'll use the following setup:
- Nginx Load Balancer : 192.168.10.10
- App server1 : 192.168.10.11
- App server2 : 192.168.10.12
Install Nginx Server
First, you will need to install Nginx on all servers. You can install it with the following command:
# apt-get install nginx -y
Once installed, start the Nginx service and enable it to start at system reboot:
# systemctl start nginx
# systemctl enable nginx
Set up Application Servers
Next, you will need to setup both application servers.
On the first application server, remove the default index.html file and create a new one:
# rm -rf /usr/share/nginx/html/index.html
# nano /usr/share/nginx/html/index.html
Add the following lines:
<html>
<title>First Application Server</title>
<body>
This is my first application server
</body>
</html>
Save and close the file.
One the second application server, remove the default index.html file and create a new file:
# rm -rf /usr/share/nginx/html/index.html
# nano /usr/share/nginx/html/index.html
Add the following lines:
<html>
<title>Second Application Server</title>
<body>
This is my second application server
</body>
</html>
Save and close the file.
Set up an Nginx Load Balancer
Next, you will need to set up an load balancer server that distributes load among both application server.
First, remove the Nginx default configuration file and create a new load balancer configuration file:
# rm -rf /etc/nginx/sites-enabled/default
# nano /etc/nginx/conf.d/load-balancing.conf
Add the following lines:
upstream backend {
server 192.168.10.11;
server 192.168.10.12;
}
server {
listen 80;
server_name loadbalancing.example.com;
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.
Use the following lines if you want to use the Least_conn method:
upstream backend {
least_conn;
server 192.168.10.11;
server 192.168.10.12;
}
Use the following lines if you want to use the Ip_hash method:
upstream backend {
ip_hash;
server 192.168.10.11;
server 192.168.10.12;
}
Save and close the file then verify the Nginx for any syntax error with the following command:
# nginx -t
You should see the following output:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
Next, restart the Nginx service to apply the changes:
# systemctl restart nginx
Verify Load Balancing
Now, open your web brower and access the load balancing server using the URL http://loadbalancing.example.com. You will be redirected to the application server1:
Now, refresh the page continuously after some time your application should be loaded from the second application server as shown below:
Conclusion
Congratulations! You have successfully set up an Nginx load balancer on Ubuntu 20.04 server.
You can now add multiple servers on load balancer to distribute the load between them.