GICT knowledge base
How to Setup Nginx Load Balancing?
Published
If you are using Nginx as your web server, you can setup load balancing technique using Nginx. It is very useful technology to distribute the incoming network across a number of servers. Using Nginx load balancing you can lower the response time, reduce the resource usage and avoid the load spike of the server. Nginx load balancing is one of the most efficient mechanisms which is able to achieve the redundancy of full application and is very easy without any complications to setup. You can set up with the round robin algorithm, which is fairly easy to implement. By using the round robin method, it will forward all requests to the corresponding server included in the Nginx configuration. Let’s have a look into the setup of the Nginx load balancing.
Let’s start with the installation and initial configuration of the Nginx.
1) Login to your server via SSH as root.
# ssh root@IP
2) Always it is a good practice to follow before any new installations make sure that the currently installed packages are up to date.
# yum update all -y
3) Install Nginx web server.
# yum install nginx -y
4) After the installation checks the status of the nginx.
# service nginx status
5) Append the load balancing configuration. Open the nginx configuration file of your domain with your favorite text editor.
# vi /etc/nginx/sites-available/domain.com.conf
Now append the load balancer configuration at the top of the file.
upstream loadbalancer {
server server1.domain.com;
server server2.domain.com;
server server3.domain.com;
}
Here I have used the word server with the number, instead of this, you can use your server hostname. Make sure all the servers mentioned above must have Nginx installed and listening on 80 port.
5) Enable the upstream module. In the same configuration file itself, you need to add the upstream module on the virtual host configuration.
server {
location / {
proxy_pass http://loadbalancer;
}
}
6) After this, save the file and restart the Nginx server to get reflected by the changes made.
# service nginx restart
The changes we made using the upstream module will equally distribute all the incoming traffic among the three servers.
7) Weight Balancing, it can also be configured it distribute the traffic according to your need. That is, you can assign the servers to get the traffic accordingly. It can be done using an option called Weight balancing.
As an example refer to the following entry.
upstream loadbalancer {
server server1.domain.com weight=2;
server server2.domain.com weight= 4;
server server3.domain.com weight=5;
}
Here server2.domain.com will get twice as the traffic as that of the server1.domain.com and the server3.domain.com will get five times above the traffic as that of the server1.domain.com
Likewise, you can configure Nginx Load balancing.
If you need any further assistance please contact our support department.
Related guides
How to Restrict an SSH Key to One Command
Restrict an SSH key to one command with authorized_keys options. Protect the command and check that unwanted shell and forwarding access are denied.
Linux and CommandsSelf-Hosted Video Processing With FFmpeg: A Practical Guide
Learn how to self-host video processing on a VPS using FFmpeg, with practical commands for conversion, compression, thumbnails, and automated workflows.
Linux and CommandsHow to use nohup: Commands in Linux That Survive Terminal Closes
Keep commands running after you close the terminal. nohup examples with wget, Python, rsync, and Docker on any VPS.
Linux and CommandsHow to Fix “Too Many Open Files” Error on Linux Servers
Learn how to fix the "Too Many Open Files" error on Linux servers by understanding file descriptors, adjusting limits, and diagnosing leaks effectively.
