How to configure Nginx for load balancing

Load balancing is a very useful technique for distributing incoming network traffic across multiple servers. With this method, you can reduce resource usage, improve response times, and avoid server overload. In this tutorial, we will guide you through the steps to configure load balancing in Nginx on Linux.

Load balancing in Nginx is one of the most efficient options available to achieve full application redundancy and is relatively easy and quick to set up. We’ll set up Nginx load balancing using Robin’s circular mechanisms. This way it will forward all requests to the appropriate server included in the Nginx configuration.

Let’s start by installing and configuring.

Log in to the server as root

ssh [email protected]_Address

and make sure all packages installed on the server are up to date

apt-get update && apt-get upgrade

We need an Nginx web server installed on a Linux VPS. Run the following command to install Nginx

apt-get install nginx

After installation, check that the web server is active

service nginx status

● 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)

Now open your site’s Nginx config file with the vim text editor

vim /etc/nginx/sites-available/yourdomain.ru.conf

and add load balancing config to the top of the file

upstream loadbalancer {
server vps1.yourdomain.ru;
server vps2.yourdomain.ru;
server vps3.yourdomain.ru;
}

You must set port 80 on all the servers listed above for Nginx.

In the same configuration file yourdomain.ru.conf we need to add the upstream module to the virtualhost configuration

server {
location / {
proxy_pass http://loadbalancer;
}
}

Save the file and restart Nginx for the changes to take effect.

service nginx restart

This configuration will equally distribute all incoming traffic across all three servers (vps1.yourdomain.ru, vps2.yourdomain.ru, vps3.yourdomain.ru). Nginx can also be configured to distribute traffic more efficiently. It comes with balancing options like weight balancing, max fails and IP hash balancing.

Weight balancing

We can use this option to specify some of the traffic will be distributed to each of the servers that we have listed upstream.

For example:

upstream loadbalancer {
server vps1.yourdomain.ru weight=1;
server vps2.yourdomain.ru weight=2;
server vps3.yourdomain.ru weight=5;
}

In this example, vps2.yourdomain.ru will receive twice as much traffic as vps2.yourdomain.ru and vps3.yourdomain.ru will receive five times more traffic than vps1.yourdomain.ru.

Max Fails

If you are using the default Nginx settings, it will send data to the servers even if they crashed. We can use the Max fails option to prevent this from happening.

upstream loadbalancer {
server vps1.yourdomain.ru max_fails=4  fail_timeout=20s;
server vps2.yourdomain.ru weight=2;
server vps3.yourdomain.ru weight=4;
}

In the above example, Nginx will try to connect to vps1.yourdomain.ru and if it hasn’t responded for more than 20 seconds, it will try again. After four attempts, vps1.yourdomain.ru will be considered not working.

IP Hash Balancing

With this method, visitors will always be sent to the same server. Thus, if a visitor received the content of vps1.yourdomain.ru, then it will always be transmitted to this server, unless the servers are down or unavailable.

upstream loadbalancer {
ip_hash;
server vps1.yourdomain.ru;
server vps2.yourdomain.ru;
server vps3.yourdomain.ru down;
}

In this example, vps3.yourdomain.ru is known to be unavailable and is marked as crashed.

PS. If you liked this post, please share it with your friends on social networks using the buttons on the left side of the post, or just leave a comment below. Thank you in advance.

Sidebar