Nginx as a Load balancer (Part-III)
Tags: nginx, loadbalancer, architecture, scaling
Published: 2019-05-02 10:30:10

---
title: Nginx as a Load balancer (Part-III)
published: true
description: Nginx as a load balancer. The first step in scaling your application. Let's keep it simple.
tags: nginx, loadbalancer, architecture, scaling,
series: Nginx
---

Nginx as a load balancer, this is 3rd part of the series([Part I](https://dev.to/rshiva/welcome-to-nginx-part-i-58mn) and [Part II](https://dev.to/rshiva/welcome-to-nginx-part-ii-32kmn)).  The first step in scaling your server. Let's keep it simple.

## **What is load balancer?**

Loadbalancer is a method used for distributing traffic across the servers. Before we get into details of load balancing, there are two ways we can scale the server. First, Vertical scaling, in this we add more computing power i.e by increasing RAM and CORE to an existing server but this will be expensive after a certain point. Second, Horizontal scaling is to add more servers of the same or different configuration and install the required application to run our web app. Below image gives a gist about vertical vs horizontal scaling

![Vertical vs horizontal](https://thepracticaldev.s3.amazonaws.com/i/zjpwj28f1lptdfz6w8ej.png)

## **Why do you need it?**

To increase the application performance, availability and to decrease the latency when faced with heavy traffic. When our server cannot handle too many requests or the number of users visiting our website increases. Our application can get slow and users can get annoyed since the time taken to do any task increases. This is bad user experience. This is when we scale our servers.


## **Nginx as Load Balancer**

Nginx is a software load balancer. It can be used as HTTP load balancer efficiently by distributing the request across multiple application servers which will increase the app availability (even if one server goes down), performance and scalability. There are hardware load balancers which are quite expensive & require separate resources for maintenance and debugging, which can be become quite a tedious task. Successful [case studies](https://www.nginx.com/success-stories/) of companies using nginx as a load balancer and some companies replaced their hardware load balancer with nginx.

First, we will need a dedicated server where we run only nginx whose job will be just to forward the request upstream. Let's add three more application servers. Instead of pointing the application to our single server we now point our domain or first landing request to nginx load balancer. Nginx will decide where the request should be forwarded based on the techniques mentioned below.

![Load balancer architecture](https://thepracticaldev.s3.amazonaws.com/i/gfa0sb591p49r7sz3fb7.png)
Image showing our architecture

### **Follow the below steps to get started -**


First, let's create a config file called load_balancer.conf inside 

        cd /etc/nginx/conf
        sudo touch load_balancer.conf

Open the load_balancer.conf and add the below configuration. 

        http {
            upstream wayne_tech {
                server server1.com;
                server server2.com;
                server server3.com;
            }

            server {
                listen 80;

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

        

Let's save this configuration file and include this file in our main configuration file and restart the server. Refer [Part II](https://dev.to/rshiva/welcome-to-nginx-part-ii-32kmn) post if you need to know how it's done.

server1.com, server2.com  and server3.com, IP configuration need to be added in /etc/hosts. Let's do that.

        sudo vi /etc/hosts
        
        192.168.1.101   server1.com
        192.168.1.102   server2.com
        192.168.1.103   server3.com

Now that all the configurations are done. When any request comes to the web app it lands on the load balancer and it decides where to forward the request.

There are different load balancing techniques supported by nginx.

### **1) round-robin(default)**
By default, Round-robin algorithm is used, in which each request is given to upstream server in sequence. i.e the first request will land on server1 and second request will land on server2 and so on.


        upstream wayne_tech {
            server server1.com;
            server server2.com;
            server server3.com;
        }

### **2) least-connected**

In this technique, nginx will not overload the server which is already processing a lot of requests; instead, it directs the requests to a less busy server. Nginx will compare each active connection with servers and send the request to a least active connection. We just need to add a least_conn directive in our upstream configuration

        upstream wayne_tech {
            least_conn;
            server server1.com;
            server server2.com;
            server server3.com;
        }

    
### **3) ip-hash**

If you need to map a client to a particular application server,i.e to keep client persistent or to maintain a state, ip-hash can be used. Requests are distributed based on the client's IP address which is used as a hashing key to serve the request to the server. This will make sure that the same client requests are directed to the same server unless that server is down.

       upstream wayne_tech {
            ip_hash;
            server server1.com;
            server server2.com;
            server server3.com;
        }



### **4) Weighted load balancing** 

In this method, we give weights to the servers. We can use this when our servers are not identical in configuration. For example, server1 has 16GB RAM and 8 CORE processor and whereas the other two servers have an 8GB and 4 CORE processor. Below configuration shows weight {6, 2, 2} which indicates that for every 10 requests, 60% percent of the request is given to server1 and 20% of the request is directed to server2 and remaining 20% to server3. 

        upstream wayne_tech {
            server server1.com weight=6;
            server server2.com weight=2;
            server server3.com weight=2;
        }

### **Conclusion:**

This is the last post on nginx series. Now that you know how to set up and install a server. And if your application receiving loads of traffic and use one of these techniques to distribute the load across the server. I encourage you to dig in the nginx documentation to unwrap its power and there is an enterprise edition of nginx plus which gives you more features. I have been using Nginx for almost a decade I never felt the need for enterprise edition even though we are serving millions of request per server per day. Reach out to me if you need any help. That's all folks.