Nginxload balancingtutorial

LearningNginxload balancing 策略, configurationmethod, healthycheck and session保持, improvingsystem availability and performance

load balancingoverview

what is load balancing?

load balancing is adistributedtechniques, 它将networkrequest or traffic均匀地分发 to many 个server on , 以improvingsystem availability, reliability and performance. in Webapplicationin, load balancing可以 has 效避免单点failure, improvingsystem concurrentprocessingcapacity.

load balancing working principles

  1. 客户端向load balancing器发送request
  2. load balancing器根据预设 策略选择一个 after 端server
  3. load balancing器将request转发给选定 after 端server
  4. after 端serverprocessingrequest并生成response
  5. load balancing器接收 after 端server response
  6. load balancing器将response返回给客户端

load balancing 优势

Nginxload balancing策略

Nginxproviding了 many 种load balancing策略, 可以根据不同 场景选择合适 策略.

1. 轮询 (Round Robin)

轮询 is Nginx默认 load balancing策略, 它会将request按照顺序依次分配给 after 端server.

特点:

  • simple 易用, 不需要额 out configuration
  • 适合所 has after 端serverperformance相近 场景
  • 不考虑server practicalloadcircumstances

2. IP哈希 (IP Hash)

IP哈希策略会根据客户端 IP地址计算哈希值, 然 after 将request分配给 for 应 after 端server.

特点:

  • 可以implementationsession保持, 同一客户端 request会被分配 to 同一台server
  • 适合需要session保持 application场景
  • such as果 after 端server数量变化, 可能会导致哈希重 new 计算, 影响session保持

3. 最 few 连接 (Least Connections)

最 few 连接策略会将request分配给当 before connections最 few after 端server.

特点:

  • 考虑server practicalloadcircumstances
  • 适合 after 端serverperformancediff较 big 场景
  • 可以更合理地分配load

4. 权重 (Weighted)

权重策略允许 for 不同 after 端server设置不同 权重值, 权重值越 high server会被分配更 many request.

特点:

  • 可以根据server performance设置不同 权重
  • 适合 after 端serverperformancediff较 big 场景
  • 可以 and 轮询, IP哈希, 最 few 连接etc.策略结合using

5. 最 few 时间 (Least Time)

最 few 时间策略会将request分配给response时间最 short after 端server.

特点:

  • 考虑server response时间
  • 适合 for response时间要求较 high 场景
  • 需要Nginx Plusversionsupport

6. common哈希 (Generic Hash)

common哈希策略可以根据指定 键 (such asURL, requestparameteretc.) 计算哈希值, 然 after 将request分配给 for 应 after 端server.

特点:

  • 可以根据业务requirements自定义哈希键
  • 适合需要基于specific规则分配request 场景
  • 需要Nginx Plusversionsupport

load balancingconfigurationexample

1. basicload balancingconfiguration

upstream backend {
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

2. 带权重 load balancingconfiguration

upstream backend {
    server localhost:8080 weight=3;
    server localhost:8081 weight=2;
    server localhost:8082 weight=1;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

3. IP哈希load balancingconfiguration

upstream backend {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

4. 最 few 连接load balancingconfiguration

upstream backend {
    least_conn;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

5. 带权重 最 few 连接load balancingconfiguration

upstream backend {
    least_conn;
    server localhost:8080 weight=3;
    server localhost:8081 weight=2;
    server localhost:8082 weight=1;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

healthycheckconfiguration

what is healthycheck?

healthycheck is amechanism, 用于检测 after 端server is 否正常run. such as果 after 端server出现failure, load balancing器会自动将其 from server池in移除, 直 to 它restore正常.

basichealthycheckconfiguration

upstream backend {
    server localhost:8080 max_fails=3 fail_timeout=30s;
    server localhost:8081 max_fails=3 fail_timeout=30s;
    server localhost:8082 max_fails=3 fail_timeout=30s;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

healthycheckparameter说明

advancedhealthycheck (Nginx Plus)

Nginx Plusproviding了更advanced healthycheckfunctions, 可以发送specific HTTPrequest来check after 端server healthystatus.

upstream backend {
    zone backend 64k;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
    
    health_check interval=5s fails=3 passes=2;
    health_check uri=/health;
    health_check match=healthy;
}

match healthy {
    status 200;
    header Content-Type = "application/json";
    body ~ "{\"status\":\"ok\"}";
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

session保持configuration

what is session保持?

session保持 is 指将同一客户端 request始终分配给同一台 after 端server mechanism. in 某些application场景in, 客户端 and server之间会建立session, 需要确保 after 续 request仍然发送 to 同一台server on .

1. IP哈希session保持

upstream backend {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

2. 基于Cookie session保持

可以usingNginx stickymodule or 第三方moduleimplementation基于Cookie session保持.

upstream backend {
    sticky cookie srv_id expires=1h domain=example.com path=/;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

3. 基于URL session保持

可以usingNginx hashmoduleimplementation基于URL session保持.

upstream backend {
    hash $request_uri consistent;
    server localhost:8080;
    server localhost:8081;
    server localhost:8082;
}

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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

实践case: configurationhigh availability Webapplicationload balancing

fake设我们 has 一个Webapplication, deployment in 3台server on , 需要configurationNginxload balancing来improvingsystem availability and performance:

upstream webapp {
    # using最 few 连接策略
    least_conn;
    
    # configuration after 端server, 设置权重 and healthycheckparameter
    server 192.168.1.10:8080 weight=3 max_fails=3 fail_timeout=30s;
    server 192.168.1.11:8080 weight=2 max_fails=3 fail_timeout=30s;
    server 192.168.1.12:8080 weight=1 max_fails=3 fail_timeout=30s;
}

server {
    listen 80;
    server_name webapp.example.com;
    
    # 访问log and errorlog
    access_log /var/log/nginx/webapp.access.log main;
    error_log /var/log/nginx/webapp.error.log warn;
    
    # load balancingconfiguration
    location / {
        proxy_pass http://webapp;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # proxy缓冲区设置
        proxy_buffers 16 16k;
        proxy_buffer_size 32k;
        
        # 超时设置
        proxy_connect_timeout 60s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;
    }
    
    # healthycheck端点
    location /health {
        proxy_pass http://webapp/health;
        proxy_set_header Host $host;
    }
}

这个configurationusing了最 few 连接策略, 并 for 不同 after 端server设置了不同 权重. 同时, configuration了healthycheckparameter, 当server出现failure时, 会自动将其 from server池in移除.

互动练习

1. Nginx默认 load balancing策略 is what?

A. 轮询 (Round Robin)
B. IP哈希 (IP Hash)
C. 最 few 连接 (Least Connections)
D. 权重 (Weighted)

2. 以 under 哪些load balancing策略可以implementationsession保持? ( many 选)

A. 轮询 (Round Robin)
B. IP哈希 (IP Hash)
C. 最 few 连接 (Least Connections)
D. 基于Cookie session保持
E. 基于URL session保持

3. 实践练习: configurationload balancing

请configuration一个Nginxload balancing, 将 for api.example.com request分发 to 3台 after 端server (192.168.1.10:8080, 192.168.1.11:8080, 192.168.1.12:8080) . 要求:

  • using最 few 连接策略
  • for server设置不同 权重 (3:2:1)
  • configurationhealthycheckparameter (max_fails=3, fail_timeout=30s)
  • 设置适当 HTTP头 and 超时parameter
  • configuration访问log and errorlog

summarized and 展望

through本tutorial, 您已经Learning了Nginxload balancing 策略, configurationmethod, healthycheck and session保持. load balancing is 构建high availability, high performanceWebsystem important techniques, 它可以:

in after 续 tutorialin, 我们将深入LearningNginx 静态fileservice, securityconfiguration, performanceoptimizationetc.advanced features, helping您全面MasterNginx usingtechniques.