different types of load balancing in nginx3 min read
Hi All, this document is about different types of load balancing in nginx.
Nginx is one of the best and commonly used webserver in the world. It famous more for its capability of handling the requests.
It supports various modules that can be used for real-time examples. Nginx can be used as a reverse proxy, mail proxy, HTTP cache, and even a load balancer.
Today I’m going to brief you about the load balancing techniques which are supported by Nginx.
Nginx supports various support of load balancer types such as
- least connections
- IP hash
Let’s look into each technique separately
Round Robin is the default load-balancing method, this doesn’t require any kind of configuration.
Above is the sample configuration which works with round-robin. Round Robin works as an equal number of requests are given to the number of servers listed the upstream. It will better always to have the same configuration of servers. If any of the servers/services are down on the listed upstream nginx continues to send the request to those servers. This results in a 502 gateway error for the users.
In such cases, the configuration can be tweaked with max_fail and fail_timeout.
fail_timeout – This sets the time duration of fails mush to happen for the server to be marked as the server is unavailable to handle requests.
max_fail – is the number of fail attempts that happened during the fail_timeout to mark server is unavailable.
In this method of load balancing, the load balancer sends requests to the server which have less active connections. In this method, not all the servers loaded with more connection. Nginx will handle it internally.
This configuration is made by adding the least_conn derivative in the upstream section.
IP Hash works for HTTP only and the has is based on the client IP address. This is enabled by adding ip_hash derivative in the upstream section. In IPv4 the hash is based on the first 3 octets of the address. So whenever the IP address changes dynamically router reboot or reconnection nginx checks only the first 3 octets and the request still goes to the same connected server as before.
This method is not good when the customer is expecting a huge request coming from a specific series of IP addresses. This will lead to server unavailability.
Still to overcome these scenarios use the max_fail concept for the upstream servers.
This is another method used in load balancer which you can choose when you want to split the traffic across the server based on some values.
Lets you want to split requests on one server with 75% and other with 25%, below is the configuration for that.
this type of configuration helps when the equal specification is not used in the load balancer.
Hope you understood nginx load balancing methods.