How to Use Nginx as an HTTP Load Balancer in Linux

Setup Nginx HTTP Load Balancer in Linux

Setup Nginx HTTP Load Balancer in Linux

When it comes to setting up multiple application servers for redundancy, load balancing is a commonly used mechanism for efficiently distributing incoming service requests or network traffic across a group of back-end servers.

Load balancing has several advantages including increased application availability through redundancy, increased reliability and scalability (more servers can be added in the mix when traffic increases). It also brings about improved application performance and many other benefits.

Nginx can be deployed as an efficient HTTP load balancer to distribute incoming network traffic and workload among a group of application servers, in each case returning the response from the selected server to the appropriate client.

The load balancing methods supported by Nginx are:

  • round-robin – which distributes requests to the application servers in a round-robin fashion. It is used by default when no method is specified,
  • least-connected – assigns the next request to a less busy server(the server with the least number of active connections),
  • ip-hash – where a hash function is used to determine what server should be selected for the next request based on the client’s IP address. This method allows for session persistence (tie a client to a particular application server).

Besides, you can use server weights to influence Nginx load balancing algorithms at a more advanced level. Nginx also supports health checks to mark a server as failed (for a configurable amount of time, default is 10 seconds) if its response fails with an error, thus avoids picking that server for subsequent incoming requests for some time.

This practical guide shows how to use Nginx as an HTTP load balancer to distribute incoming client requests between two servers each having an instance of the same application.

For testing purposes, each application instance is labeled (on the user interface) to indicate the server it is running on.

Testing Environment Setup

Load Balancer:
Application server 1:
Application server 2:

On each application server, each application instance is configured to be accessed using the domain tecmintapp.lan. Assuming this is a fully-registered domain, we would add the following in the DNS settings.

A Record   		@

This record tells client requests where the domain should direct to, in this case, the load balancer ( The DNS A records only accept IPv4 values. Alternatively, the /etc/hosts file on the client machines can also be used for testing purposes, with the following entry.  	tecmintapp.lan

Setting Up Nginx Load Balancing in Linux

Before setting up Nginx load balancing, you must install Nginx on your server using the default package manager for your distribution as shown.

$ sudo apt install nginx   [On Debian/Ubuntu]
$ sudo yum install nginx   [On CentOS/RHEL]   

Next, create a server block file called /etc/nginx/conf.d/loadbalancer.conf (give a name of your choice).

$ sudo vi /etc/nginx/conf.d/loadbalancer.conf

Then copy and paste the following configuration into it. This configuration defaults to round-robin as no load balancing method is defined.

upstream backend {
    server {
        listen      80 default_server;
        listen      [::]:80 default_server;
        server_name tecmintapp.lan;

        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;

In the above configuration, the proxy_pass directive (which should be specified inside a location, / in this case) is used to pass a request to the HTTP proxied servers referenced using the word backend, in the upstream directive (used to define a group of servers). Also, the requests will be distributed between the servers using a weighted round-robin balancing mechanism.

To employ the least connection mechanism, use the following configuration

upstream backend {

And to enable ip_hash session persistence mechanism, use:

upstream backend {

You can also influence the load balancing decision using server weights. Using the following configuration, if there are six requests from clients, the application server will be assigned 4 requests and 2 will go

upstream backend {
        server	weight=4;

Save the file and exit it. Then ensure the Nginx configuration structure is correct after adding the recent changes, by running the following command.

$ sudo nginx -t

If the configuration is OK, restart and enable the Nginx service to apply the changes.

$ sudo systemctl restart nginx
$ sudo systemctl enable nginx

Testing Nginx Load Balancing in Linux

To test the Nginx load balancing, open a web browser and use the following address to navigate.


Once the website interface loads, take note of the application instance that has loaded. Then continuously refresh the page. At some point, the app should be loaded from the second server indicating load balancing.

Check Nginx Load Balancing in Linux

Popular posts from this blog

Don't like IDEs? Try

Man Uses 99 Smartphones to Fool Google Maps and Create a Fake Traffic Jam Here is some pic ..

Top 10 In-Demand programming languages to learn in this Year