How to Use Nginx as an HTTP Load Balancer in Linux | What is Load Balancer?

Use Nginx as an HTTP Load Balancer in Linux
web server - nginx

What is load balancer and what are the methods to implement load balancing?

Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

It is possible to use Nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with Nginx.

How to Use Nginx as an HTTP Load Balancer in Linux | What is Load Balancer? 1

Load balancing can be implemented using different methods

Use Nginx as an HTTP Load Balancer in Linux

The below load balancing mechanisms (or methods) are supported in Nginx:

Round-Robin

Requests to the application servers are distributed in a round-robin fashion,
least-connected — next request is assigned to the server with the least number of active connections,
ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

Configure Load Balancer in Nginx

The configuration to implement the Load Balancer

MacBookAir-Linux:~$ cd /etc/nginx/sites-enabled
MacBookAir-Linux:~$ vim load-balancer.conf
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}

Least Connected Load Balancing

Another load balancing discipline is least-connected. Least-connected allows controlling the load on application instances more fairly in a situation when some of the requests take longer to complete.

With the least-connected load balancing, Nginx will try not to overload a busy application server with excessive requests, distributing the new requests to a less busy server instead.

How to activate Least Connected Load Balancer

MacBookAir-Linux:~$ cd /etc/nginx/sites-enabled
MacBookAir-Linux:~$ vim load-balancer.conf

upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}



Session Persistence


If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the IP-hash load balancing mechanism can be used.
This method ensures that the requests from the same client will always be directed to the same server except when this server is unavailable.

MacBookAir-Linux:~$ cd /etc/nginx/sites-enabled
MacBookAir-Linux:~$ vim load-balancer.conf
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}

Learn more Algorithm for Load Balancer

techouse

I love helping beautiful people like you. I love hanging out with my dogs.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.