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/

and add load balancing config to the top of the file

                      upstream loadbalancer {

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

In the same configuration file we need to add the upstream module to the virtualhost configuration

                      server {
location / {
proxy_pass https://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 (,, 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 weight=1;
server weight=2;
server weight=5;

In this example, will receive twice as much traffic as and will receive five times more traffic than

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 max_fails=4  fail_timeout=20s;
server weight=2;
server weight=4;

In the above example, Nginx will try to connect to and if it hasn’t responded for more than 20 seconds, it will try again. After four attempts, 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, then it will always be transmitted to this server, unless the servers are down or unavailable.

                      upstream loadbalancer {
server down;

In this example, 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.

Related Posts