Nginx Supports Several Types of Load Balancing Methods To Distribute Traffic Across Multiple Backend Servers
Nginx Supports Several Types of Load Balancing Methods To Distribute Traffic Across Multiple Backend Servers
servers. Here are the primary types and how to configure each:
1. Round Robin
Round Robin is the default load balancing method in Nginx. It distributes client requests evenly across
the backend servers in the order they are listed.
Configuration:
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
2. Least Connections
This method passes each request to the server with the least number of active connections.
Configuration:
http {
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
3. IP Hash
The IP Hash method uses the client’s IP address to determine which server will handle the request. This
ensures that a specific client always connects to the same server.
Configuration:
http {
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
4. Generic Hash
This method distributes requests based on a user-defined key using the hash directive. It ensures
requests with the same key go to the same server.
Configuration:
http {
upstream backend {
hash $request_uri;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
5. Least Time
This method distributes requests to the server with the lowest average response time and least number
of active connections. It requires the stream module.
Configuration:
stream {
upstream backend {
least_time connect;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
proxy_pass backend;
Advanced Features:
Health Checks:
To ensure that Nginx only forwards requests to healthy servers, you can configure active health checks.
Configuration:
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
Sticky Sessions:
Sticky sessions ensure that requests from the same client are always directed to the same backend
server.
Configuration:
http {
upstream backend {
sticky;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
server {
listen 80;
location / {
proxy_pass http://backend;
Summary:
• Least Connections: Directs traffic to the server with the fewest active connections.
• IP Hash: Ensures requests from the same client IP always go to the same server.
• Least Time: Uses response time to distribute requests to the fastest server.