Tutorials How to configure load balancing using Nginx

How to configure load balancing using Nginx

Advantages of load balancing

Load balancing is an excellent way to scale out your application and increase its performance and redundancy. Nginx, a popular web server software, can be configured as a simple yet powerful load balancer to improve your servers resource availability and efficiency. Nginx acts as a single entry point to a distributed web application working on multiple separate servers.

Load balancing on a web farm

This guide describes the advantages of load balancing. Learn how to set up load balancing with nginx for your cloud servers.

As a prerequisite, you’ll need to have at least two hosts with a web server software installed and configured to see the benefit of the load balancer. If you already have one web host set up, duplicate it by creating a custom image and deploy it onto a new server at your UpCloud control panel.

Installing nginx

The first thing to do is to set up a new host that will serve as your load balancer. Deploy a new instance at your UpCloud Control Panel if you haven’t already. Currently, nginx packages are available on the latest versions of CentOS, Debian and Ubuntu. So pick whichever of these you prefer.

After you have set up the server the way you like, install the latest stable nginx. Use one of the following methods.

# Debian and Ubuntu
sudo apt-get update
# Then install the Nginx Open Source edition
sudo apt-get install nginx
# CentOS
# Install the extra packages repository
sudo yum install epel-release
# Update the repositories and install Nginx
sudo yum update
sudo yum install nginx

Once installed change directory into the nginx main configuration folder.

cd /etc/nginx/

Now depending on your OS, the web server configuration files will be in one of two places.

Ubuntu and Debian follow a rule for storing virtual host files in /etc/nginx/sites-available/, which are enabled through symbolic links to /etc/nginx/sites-enabled/. You can use the command below to enable any new virtual host files.

sudo ln -s /etc/nginx/sites-available/vhost /etc/nginx/sites-enabled/vhost

CentOS users can find their host configuration files under /etc/nginx/conf.d/ in which any .conf type virtual host file gets loaded.

Check that you find at least the default configuration and then restart nginx.

sudo systemctl restart nginx

Test that the server replies to HTTP requests. Open the load balancer server’s public IP address in your web browser. When you see the default welcoming page for nginx the installation was successful.

Nginx default welcome page.

If you have trouble loading the page, check that a firewall is not blocking your connection. For example on CentOS 7 the default firewall rules do not allow HTTP traffic, enable it with the commands below.

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload

Then try reloading your browser.

Configuring nginx as a load balancer

When nginx is installed and tested, start to configure it for load balancing. In essence, all you need to do is set up nginx with instructions for which type of connections to listen to and where to redirect them. Create a new configuration file using whichever text editor you prefer. For example with nano:

sudo nano /etc/nginx/conf.d/load-balancer.conf

In the load-balancer.conf you’ll need to define the following two segments, upstream and server, see the examples below.

# Define which servers to include in the load balancing scheme. 
# It's best to use the servers' private IPs for better performance and security.
# You can find the private IPs at your UpCloud control panel Network section.
http {
   upstream backend {
      server 10.1.0.101; 
      server 10.1.0.102;
      server 10.1.0.103;
   }

   # This server accepts all traffic to port 80 and passes it to the upstream. 
   # Notice that the upstream name and the proxy_pass need to match.

   server {
      listen 80; 

      location / {
          proxy_pass http://backend;
      }
   }
}

Then save the file and exit the editor.

Next, disable the default server configuration you earlier tested was working after the installation. Again depending on your OS, this part differs slightly.

On Debian and Ubuntu systems you’ll need to remove the default symbolic link from the sites-enabled folder.

sudo rm /etc/nginx/sites-enabled/default

CentOS hosts don’t use the same linking. Instead, simply rename the default.conf in the conf.d/ directory to something that doesn’t end with .conf, for example:

sudo mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.disabled

Then use the following to restart nginx.

sudo systemctl restart nginx

Check that nginx starts successfully. If the restart fails, take a look at the  /etc/nginx/conf.d/load-balancer.conf you just created to make sure there are no mistypes or missing semicolons.

When you enter the load balancer’s public IP address in your web browser, you should pass to one of your back-end servers.

Load balancing methods

Load balancing with nginx uses a round-robin algorithm by default if no other method is defined, like in the first example above. With round-robin scheme each server is selected in turns according to the order you set them in the load-balancer.conf file. This balances the number of requests equally for short operations.

Least connections based load balancing is another straightforward method. As the name suggests, this method directs the requests to the server with the least active connections at that time. It works more fairly than round-robin would with applications where requests might sometimes take longer to complete.

To enable least connections balancing method, add the parameter least_conn to your upstream section as shown in the example below.

upstream backend {
   least_conn;
   server 10.1.0.101; 
   server 10.1.0.102;
   server 10.1.0.103;
}

Round-robin and least connections balancing schemes are fair and have their uses. However, they cannot provide session persistence. If your web application requires that the users are subsequently directed to the same back-end server as during their previous connection, use IP hashing method instead. IP hashing uses the visitors IP address as a key to determine which host should be selected to service the request. This allows the visitors to be each time directed to the same server, granted that the server is available and the visitor’s IP address hasn’t changed.

To use this method, add the ip_hash -parameter to your upstream segment like in the example underneath.

upstream backend {
   ip_hash;
   server 10.1.0.101; 
   server 10.1.0.102;
   server 10.1.0.103;
}

In a server setup where the available resources between different hosts are not equal, it might be desirable to favour some servers over others. Defining server weights allows you to further fine-tune load balancing with nginx. The server with the highest weight in the load balancer is selected the most often.

upstream backend {
   server 10.1.0.101 weight=4; 
   server 10.1.0.102 weight=2;
   server 10.1.0.103;
}

For example in the configuration shown above the first server is selected twice as often as the second, which again gets twice the requests compared to the third.

Load balancing with HTTPS enabled

Enable HTTPS for your site, it is a great way to protect your visitors and their data. If you haven’t yet implemented encryption on your web hosts, we highly recommend you take a look at our guide for how to install Let’s Encrypt on nginx.

To use encryption with a load balancer is easier than you might think. All you need to do is to add another server section to your load balancer configuration file which listens to HTTPS traffic at port 443 with SSL.  Then set up a proxy_pass to your upstream segment like with the HTTP in the previous example above.

Open your configuration file again for edit.

sudo nano /etc/nginx/conf.d/load-balancer.conf

Then add the following server segment to the end of the file.

server {
   listen 443 ssl;
   server_name domain_name;
   ssl_certificate /etc/letsencrypt/live/domain_name/cert.pem;
   ssl_certificate_key /etc/letsencrypt/live/domain_name/privkey.pem;
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

   location / {
      proxy_pass http://backend;
   }
}

Then save the file, exit the editor and restart nginx again.

sudo systemctl restart nginx

Setting up encryption at your load balancer when you are using the private network connections to your back-end has some great advantages.

  • As only your UpCloud servers have access to your private network, it allows you to terminate the SSL at the load balancer and thus only passing forward HTTP connections.
  • It also greatly simplifies your certificate management. You can obtain and renew the certificates from a single host.

With the HTTPS-enabled you also have the option to enforce encryption to all connections to your load balancer. Simply update your server segment listening to port 80 with a server name and a redirection to your HTTPS port. Then remove or comment out the location portion as it’s no longer needed. See the example below.

server {
   listen 80;
   server_name domain_name;
   return 301 https://$server_name$request_uri;

   #location / {
   #   proxy_pass http://backend;
   #}
}

Save the file again after you have made the changes. Then restart nginx.

sudo systemctl restart nginx

Now all connections to your load balancer will be served over an encrypted HTTPS connection. Requests to the unencrypted HTTP will be redirected to use HTTPS as well. This provides a seamless transition into encryption. Nothing is required from your visitors.

Health checks

In order to know which servers are available, nginx’s implementations of reverse proxy includes passive server health checks. If a server fails to respond to a request or replies with an error, nginx will note the server has failed. It will try to avoid forwarding connections to that server for a time.

The number of consecutive unsuccessful connection attempts within a certain time period can be defined in the load balancer configuration file. Set a parameter max_fails to the server lines. By default, when no max_fails is specified, this value is set to 1. Optionally setting the max_fails to 0 will disable health checks to that server.

If max_fails is set to a value greater than 1 the subsequent fails must happen within a specific time frame for the fails to count. This time frame is specified by a parameter fail_timeout, which also defines how long the server should be considered failed. By default, the fail_timeout is set to 10 seconds.

After a server is marked failed and the time set by fail_timeout has passed, nginx will begin to gracefully probe the server with client requests. If the probes return successful, the server is again marked live and included in the load balancing as normal.

upstream backend {
   server 10.1.0.101 weight=5;
   server 10.1.0.102 max_fails=3 fail_timeout=30s;
   server 10.1.0.103;
}

Use the health checks. They allow you to adapt your server back-end to the current demand by powering up or down hosts as required. When you start up additional servers during high traffic, it can easily increase your application performance when new resources become automatically available to your load balancer.

Conclusions

If you wish to improve your web application performance and availability, a load balancer is definitely something to consider. Nginx is powerful yet relatively simple to set up as a load balancer. Together with an easy encryption solution, such as Let’s Encrypt client, it makes for a great front-end to your web farm. Check out the documentation for upstream over at nginx.org to learn more.

When you are using multiple hosts protects your web service with redundancy, the load balancer itself can still leave a single point of failure. You can further improve high availability when you set up a floating IP between multiple load balancers. Find out more in our article on floating IPs on UpCloud.

39 thoughts on “How to configure load balancing using Nginx

  1. Thanks for your awesome documentation ! What is the best between having a nginx load balancer plus integrated swarm load balancer or using round robin dns?

    1. Hi Felix, thanks for the question! Setting up round-robin on a DNS has some drawbacks that might limit the choices of the DNS software. If you are intending to load balance between containers, Docker Swarm is easy to get started with.

      1. How do we give custom conditions in response before marking the server is off in NGINX (not Nginx plus)

          1. Yes Janne is correct- the article is not accurate. NGINX does not include health checks unless you use the commercial PLUS version.

          2. Passive health checks are available on the standard NGINX. However, the custom conditions are a feature of active health monitoring and exclusive to NGINX PLUS.

    1. Hi Agung, NGINX doesn’t include load balancer configurations by default, hence the tutorial instructs to create one according to the example.

  2. Hi Janne,
    Great article thanks. I just have a question. I am converting my current single Nginx configuration to a frond end proxy with multiple back end servers like you stated above.
    What I am not sure about is what to put on the back end servers and how to configure Nginx on those servers.
    The front end server has the content and the SSL certs configured on it but to get the benefit of the load balancing, what data and Nginx configs needs to sit on the back end servers i.e. server 10.1.0.101, , 10.1.0.102, 101.1.0.103 in your cluster?
    Thanks.

    1. Hi Waleed, thanks for the question. The beauty of Nginx is that the backend servers do not require any special configuration, just the standard Nginx web server. You can terminate the SSL at the load balancer and run the backend connections over your private network.

  3. Excellent article which covers everything developers wants to know to use nginx as load balancer.Thanks Much!

  4. Hii.. we have setup a load balancer with 2 servers. We are facing issues while streaming and playing back the same video. If the request is going to one of the servers suppose S1 and for playback the request is going to the server S2 ,in that case we are not able to playback the video. And since we are using the load balancer we can have the request on any of the servers irrespective on which the LB decides.
    So can we have any ways where we can have the the publish and play request going to the same server through the LB.

    1. Hi there, thanks for the question. I’d recommend trying a load balancing method that provides session persistence, e.g.

      upstream backend {
         ip_hash;
         server 10.1.0.101; 
         server 10.1.0.102;
      }
  5. Hello Janne,

    Clear explanation, nice write-up.

    We are a start-up from Finland. Horizontal scaling is not an option with us for some legacy software licensing costs. However, we can deploy two application servers at different ports on the same node.

    So, Is it possible load balance on same node between two ports? Something like:
    upstream backend {
    server 10.1.0.101:7777;
    server 10.1.0.101:7778;
    }

    Reply

    1. Hi Ganesh, thanks for the question. Indeed it is possible to set the backend as you described, however, you won’t get the same benefits of redundancy and scalability by deploying multiple instances of your application on the same server.

  6. Hello Janne,

    Nice explanation.

    I have a question: Is it possible to do load balancing in such a way that all the request with same cookie value goes to the same instance ?

    1. Hi Kashish, thanks for the question. Nginx does support session persistence using cookies but it’s only available in Nginx Plus. Alternatively, you could use the IP hash method to ensure that requests from the same address get to the same server.

  7. Hi Janne,
    I am seeing an unexpected behaviour with nginx, when used as a udp load balancer.

    My client and server is supposed to exchange udp packets back and forth between them for longer period. My server will be listening to a specific port and the client will initate the communication with a random port and continue to use it for all communication with the server.

    When i try to introduce NGINX in this topology to proxy the packets, i could see that after few seconds. NGINX changes the port number, which it used to communicate with the backend server for the same client. Hence the server losses the context of the session and thereby resulting in connection loss.

    This behaviour is consistent across both nginx & nginx plus.
    I have tried proxy_timeout option as well, which doesn’t solves the purpose.

    Is it a known issue of nginx ?

    1. Hi Harish, thanks for the question. It sounds like the load balancing method you are using is swapping the backend server mid stream, NGINX defaults to round-robin if no method is specified. I’d suggest using hashing with for example the remote address: hash $remote_addr; This way all consecutive packets from one client should always reach the same backend server preserving your connection.

  8. I’m a bit confused on this. I’m running on CentOS and I never had a default.conf file in my conf.d directory.

    When I create my loadbalancer.conf and try and restart nginx I get some errors around the following –
    nginx: [emerg] “http” directive is not allowed here in /etc/nginx/conf.d/loadbalancer.conf:1

    There are some other guides that are telling me to put what you have in loadbalancer.conf into my actual nginx.conf but that also is not working.. I’ve started fresh dozens of times and not sure what i’m doing wrong here.

    I get my initial nginx welcome page but as soon as I add the loadbalancer.conf and reload it fails to start.

    1. Hi Mike, thanks for the question. After a quick test, it seems Nginx on CentOS has changed the default configuration slightly. You should still set the load balancer configuration to /etc/nginx/conf.d/load-balancer.conf but without the http brackets, just upstream and server from within.

      upstream backend {
      server 10.1.0.101;
      server 10.1.0.102;
      server 10.1.0.103;
      }
      server {
      listen 80;
      location / {
      proxy_pass http://backend;
      }
      }

      You should also keep the /etc/nginx/nginx.conf but remove the server section from that file while leaving the include /etc/nginx/conf.d/*.conf; line and everything above it.

      1. Ah makes sense, I’ll try this later today.

        So if my web application has to be hit on a certain port lets say 9090.. can nginx listen on 80 and then in the server section can I put x.x.x.x:9090 and it should forward to that? Can you discuss what the “location” is for?

        1. That’s right, each server definition in the upstream can set the port as well. The location then again sets the URL prefix allowing you to proxy different parts of your site to different backends. For example:
          server {
          location / {
          proxy_pass http://backend;
          }
          location /images/ {
          root /data;
          }
          }

  9. With this method, the server logs always show the load balancer IP not the connecting client IP.
    I’ve tried adding “proxy_bind $remote_addr transparent;” and “user root; ” but I’m getting timeouts when the option is enabled.

    I’m wondering if this feature has been pulled and now only available in the Nginx plus version, or have I missed something that is required to make it work?

    1. Hi Nigel, thanks for the question. It should be possible to configure the open-source edition in a transparent mode as well and from version 1.13.8 it should no longer require Nginx to be run as root. Another option that might work is using the http_realip_module.

Leave a Reply

Your email address will not be published. Required fields are marked *

Locations

Helsinki (HQ)

In the capital city of Finland, you will find our headquarters, and our first data centre. This is where we handle most of our development and innovation.

London

London was our second office to open, and a important step in introducing UpCloud to the world. Here our amazing staff can help you with both sales and support, in addition to host tons of interesting meetups.

Singapore

Singapore was our 3rd office to be opened, and enjoys one of most engaged and fastest growing user bases we have ever seen.

Seattle

Seattle is our 4th and latest office to be opened, and our way to reach out across the pond to our many users in the Americas.