fair
The 'fair' directive enables a fair load balancing mechanism in NGINX, distributing requests to upstream servers based on their current load.
Description
The 'fair' directive is an enhancement to the traditional round-robin load balancing method found in NGINX's upstream module. It intelligently directs traffic to backend servers by considering their current load and the number of requests they are handling. This is particularly beneficial in scenarios where backend servers may have varying processing capacities or when they face temporary overloads. By tracking the number of ongoing requests per server and favoring those with fewer active connections, the 'fair' directive helps maintain better overall performance and responsiveness for users.
When utilized, this directive can be included in an 'upstream' block to define a group of backend servers. The load balancer collects metrics from each server, such as the number of active requests, and makes request distribution decisions based on these metrics. This way, if one server is busy while another is idle, the requests will be routed to the less busy server, thereby optimizing resource utilization and decreasing response time for clients. It is important to note that the 'fair' directive requires specific configuration to ensure that it correctly tracks the load and responds to it accordingly, especially when combined with health checks for backend servers.
Config Example
upstream mongrel {
fair;
server 127.0.0.1:5000;
server 127.0.0.1:5001;
server 127.0.0.1:5002;
}Ensure that proper health checks are configured for backend servers to avoid directing traffic to down servers.
Monitor server response times and performance, as the 'fair' directive relies on accurate traffic metrics to function optimally.
Be aware that setting additional parameters for weights may affect how requests are distributed, potentially leading to unequal load if not configured properly.