Nginx ~ Load balancing - rohit120582sharma/Documentation GitHub Wiki

To ensure proper speed and optimised functioning, incoming network traffic is spread across a group of services. These backend services are commonly referred to as a server pool.

It act as a traffic cop resides in front of our backend servers and distributing client requests across a group of servers in a manner that increases the speed and capacity utilisation while ensuring no one server is overloaded, which can degrade performance. If the server is not up, then the load balancer redirects traffic to the remaining online servers.

Load balancing is used to prevent servers from becoming crippled when there is an overflow of requests. A load balancer sends requests to servers that can efficiently handle them to maximize speed and performance.

It is a useful mechanism to distribute incoming traffic around several capable virtual private servers.



How load balancing occur

Nginx accepts a number of algorithms that fine tune how you want load balancing to occur. These are:

Round Robin

This is the default algorithm used when none is specified.

It basically means: routing requests to backend servers "in turn" or one after another.

This method can be tuned to make each server more less favorable to Nginx. For example, you need to make nginx02 handle more requests than nginx01. In such case, configuration may look as follows:

# nginx.conf
events {
}
http {
	upstream myproject {
		server 127.0.0.1:8000 weight=1 max_fails=4  fail_timeout=20s;
		server 127.0.0.1:8001 weight=2;
	}
	server {
		listen 80;
		server_name www.domain.com;
		location / {
			proxy_pass http://myproject;
		}
	}
}

Now 1 out of 3 requests go to nginx01 while the remaining 2 are routed to nginx02. Also nginx server will attempt to connect to http://127.0.0.1:8000/, and if it is not responding for more than 20 seconds, it will make another attempt. After the 4 attempts, http://127.0.0.1:8000/ will be considered as down.


Least connected

This can be used in situations where you cannot set a weight for backend servers, but yet you want something more precise than the round robin method.

The Least Connected algorithm instructs nginx to route requests to the backend server that is less loaded. Load is determined by the number of active connections.

This algorithm offers you also the option to set a server weight as well so that requests will be routed to the least busy server with an affinity to one or more servers.

To enable the least connection, just add the following line in configuration:

upstream backend {
	least_conn;
	server 127.0.0.1:8000;
	server 127.0.0.1:8001;
}

IP-hash

It binds the user to a specific web server according to the user's IP address hash which addresses the session persistence problem. It can be enabled by adding the following simple directive in configuration:

upstream backend {
	ip_hash;
	server 127.0.0.1:8000;
	server 127.0.0.1:8001;
}


Modifying the host headers

Beware that an application server receiving a request from a load balancer may act differently than when receiving the request from the end client.

When you navigate to the load balancer page, you will find that there is no information about the HOST and remote IP of the client.

The proxy_set_header is used to set the different HTTP headers.

Web applications often make use of the IP address of HOST. So add the following to location directive of the load balancer.

location / {
	proxy_set_header HOST $host;
	proxy_pass http://127.0.0.1:3000/;
}

Web applications often make use of the IP address that the visitor is using, perhaps for authentication purposes or to get the geographical location of the client and serve different content accordingly.

To fix this, add the following to location directive of the load balancer and then you should see the real IP address of your client browser.

location / {
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

⚠️ **GitHub.com Fallback** ⚠️