Master Nginx Load Balancing: Round Robin, Least Conn, Least Time, IP & Generic Hash
This guide outlines the five primary Nginx load‑balancing methods—Round Robin, Least Connections, Least Time (header or last_byte), IP Hash, and generic Hash—explaining how each algorithm works and showing the corresponding upstream configuration syntax for assigning server weights and parameters.
Round Robin
The default Nginx load‑balancing strategy distributes requests sequentially across servers based on each server's weight. With equal weights (default 1), the configuration looks like:
upstream backend {
server backend1.example.com;
server backend2.example.com;
}You can assign custom weights, for example:
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
}Least Connections
This method sends each request to the server with the fewest active connections.
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}Least Time
Requests are directed to the server with the smallest response latency. Latency can be measured either from the first byte received ( header) or from the complete response ( last_byte).
upstream backend {
least_time header;
server backend1.example.com;
server backend2.example.com;
}IP Hash
By hashing the client IP, Nginx consistently routes requests from the same client to the same server, helping maintain session affinity.
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}Generic Hash
This method hashes a user‑specified key, such as a variable or a combination of values, allowing custom load‑balancing criteria.
upstream backend {
hash $request_uri consistent;
server backend1.example.com;
server backend2.example.com;
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
