乐闻世界logo
搜索文章和话题

What are the load balancing strategies in Nginx? How to configure them?

2月21日 16:45

What are the load balancing strategies in Nginx? How to configure them?

Nginx provides multiple load balancing strategies that can be selected according to different business requirements. Load balancing is implemented through the upstream module, which can distribute requests to multiple backend servers.

Main Load Balancing Strategies:

1. Round Robin (Default)

Distributes requests to each server in order, suitable for scenarios with similar server performance.

nginx
upstream backend { server 192.168.1.100:8080; server 192.168.1.101:8080; server 192.168.1.102:8080; }

2. Least Connections

Assigns requests to the server with the fewest current active connections, suitable for scenarios with significant differences in request processing time.

nginx
upstream backend { least_conn; server 192.168.1.100:8080; server 192.168.1.101:8080; server 192.168.1.102:8080; }

3. IP Hash

Performs hash calculation based on client IP address, ensuring requests from the same IP are always assigned to the same server, suitable for scenarios requiring session persistence.

nginx
upstream backend { ip_hash; server 192.168.1.100:8080; server 192.168.1.101:8080; server 192.168.1.102:8080; }

4. Weighted Round Robin

Sets weights for each server, with higher-weighted servers receiving more requests, suitable for scenarios with uneven server performance.

nginx
upstream backend { server 192.168.1.100:8080 weight=3; server 192.168.1.101:8080 weight=2; server 192.168.1.102:8080 weight=1; }

5. Hash

Performs hash calculation based on a specified key, which can be variables like $request_uri, $cookie_name, etc.

nginx
upstream backend { hash $request_uri consistent; server 192.168.1.100:8080; server 192.168.1.101:8080; }

Server Status Parameters:

nginx
upstream backend { server 192.168.1.100:8080 weight=3 max_fails=3 fail_timeout=30s; server 192.168.1.101:8080 weight=2 max_fails=3 fail_timeout=30s; server 192.168.1.102:8080 down; server 192.168.1.103:8080 backup; }
  • weight: Server weight, default is 1
  • max_fails: Maximum allowed failure count, after which the server is marked as unavailable
  • fail_timeout: Failure timeout, the wait time after marking as unavailable
  • down: Marks the server as permanently unavailable
  • backup: Marks as a backup server, only used when all primary servers are unavailable
  • max_conns: Limits maximum concurrent connections

Complete Configuration Example:

nginx
http { upstream backend { least_conn; server 192.168.1.100:8080 weight=3 max_fails=3 fail_timeout=30s; server 192.168.1.101:8080 weight=2 max_fails=3 fail_timeout=30s; server 192.168.1.102:8080 weight=1 max_fails=3 fail_timeout=30s; server 192.168.1.103:8080 backup; keepalive 32; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }

Health Checks:

The open-source version of Nginx does not provide active health checks, but passive health checks can be implemented through max_fails/fail_timeout. The commercial version Nginx Plus provides active health check functionality.

Strategy Selection Recommendations:

  • Similar server performance: Use round-robin
  • Large differences in request processing time: Use least connections
  • Need session persistence: Use IP hash
  • Uneven server performance: Use weighted round-robin
  • Need distribution based on specific key: Use hash
标签:Nginx