Nginx负载均衡配置与上游服务器健康检查机制实战

Nginx作为反向代理和负载均衡器是网站运维架构的核心组件。通过upstream模块实现多后端服务器的流量分发,配合健康检查机制自动剔除故障节点,保障服务高可用。Nginx负载均衡配置涉及调度算法选择、会话保持、健康检查、故障转移和性能调优五个方面,需要在生产环境中根据业务特征进行针对性配置。

Nginx负载均衡调度算法配置

Nginx支持四种负载均衡调度算法。轮询(round-robin)是默认算法,按顺序依次分发请求。最少连接(least_conn)将请求分发到当前连接数最少的服务器。IP哈希(ip_hash)根据客户端IP进行哈希分配,实现会话保持。通用哈希(hash)可基于任意变量进行哈希分配,灵活性最高。

# /etc/nginx/conf.d/upstream.conf

# 轮询(默认)
upstream backend_round_robin {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

# 最少连接数
upstream backend_least_conn {
    least_conn;
    server 192.168.1.10:8080 weight=3;
    server 192.168.1.11:8080 weight=2;
    server 192.168.1.12:8080 weight=1;
}

# IP哈希(会话保持)
upstream backend_ip_hash {
    ip_hash;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

# 通用哈希(基于URI)
upstream backend_uri_hash {
    hash $request_uri consistent;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

# 服务器权重与状态标记
upstream backend_production {
    server 192.168.1.10:8080 weight=5 max_fails=3 fail_timeout=30s;
    server 192.168.1.11:8080 weight=5 max_fails=3 fail_timeout=30s;
    server 192.168.1.12:8080 weight=3 max_fails=3 fail_timeout=30s;
    server 192.168.1.13:8080 backup;
    server 192.168.1.14:8080 down;
}

反向代理与请求转发配置

Nginx作为反向代理转发请求时,需要正确设置proxy相关参数,确保后端服务器获取真实的客户端信息和合理的超时控制。

# /etc/nginx/conf.d/proxy.conf

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://backend_production;
        
        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;
        
        # WebSocket支持
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        
        # 超时配置
        proxy_connect_timeout 5s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # 缓冲区配置
        proxy_buffering on;
        proxy_buffer_size 16k;
        proxy_buffers 8 32k;
        proxy_busy_buffers_size 64k;
        
        proxy_redirect off;
        proxy_next_upstream error timeout http_502 http_503 http_504;
        proxy_next_upstream_tries 3;
        proxy_next_upstream_timeout 10s;
    }
    
    location /static/ {
        proxy_pass http://192.168.1.10:8080;
        proxy_set_header Host $host;
        proxy_cache_valid 200 1h;
    }
    
    location /health {
        access_log off;
        return 200 "ok\n";
        add_header Content-Type text/plain;
    }
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

主动健康检查模块配置

Nginx开源版默认只支持被动健康检查(请求失败时标记服务器不可用)。主动健康检查需要使用nginx_upstream_check_module第三方模块或Nginx Plus的商业功能。主动检查定期向后端发送探测请求,无需等待真实请求失败即可发现故障节点。

# 安装nginx_upstream_check_module
cd /usr/src
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar xzf nginx-1.24.0.tar.gz
cd nginx-1.24.0

patch -p1 < ../nginx_upstream_check_module/check_1.20.1+.patch
./configure --add-module=../nginx_upstream_check_module \
    --prefix=/etc/nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module
make && make install

# 配置主动健康检查
upstream backend_with_check {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
    
    check interval=3000 rise=2 fall=3 timeout=2000 type=http;
    check_http_send "GET /health HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}

# 健康检查状态页面
server {
    listen 8081;
    server_name localhost;
    
    location /status {
        check_status;
        access_log off;
        allow 192.168.1.0/24;
        deny all;
    }
    
    location /status.json {
        check_status json;
        access_log off;
    }
}

被动健康检查与故障转移机制

Nginx的被动健康检查通过max_fails和fail_timeout参数实现。当服务器在fail_timeout时间内连续失败max_fails次请求,Nginx将该服务器标记为不可用,在fail_timeout时间后重新尝试。

upstream backend_passive {
    server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;
    server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;
    server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;
    server 192.168.1.13:8080 weight=5 slow_start=30s;
}

location /api/ {
    proxy_pass http://backend_passive;
    proxy_next_upstream error timeout http_502 http_503 http_504;
    proxy_next_upstream_tries 3;
    proxy_next_upstream_timeout 15s;
    proxy_next_upstream non_idempotent;
    
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    
    client_max_body_size 50m;
    
    proxy_connect_timeout 3s;
    proxy_send_timeout 30s;
    proxy_read_timeout 30s;
}

负载均衡性能监控与指标采集

Nginx负载均衡的运行状态监控依赖stub_status模块和自定义日志格式。通过Prometheus采集Nginx指标,实现对请求量、响应时间、上游服务器健康状态的实时监控和告警。

# 启用stub_status
server {
    listen 127.0.0.1:8082;
    location /stub_status {
        stub_status;
        access_log off;
        allow 127.0.0.1;
        deny all;
    }
}

# 自定义日志格式记录上游服务器响应时间
log_format upstream_log '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent '
    '"$http_referer" "$http_user_agent" '
    'rt=$request_time uct=$upstream_connect_time '
    'urt=$upstream_response_time uhs=$upstream_status '
    'ucs=$upstream_cache_status';

location /api/ {
    proxy_pass http://backend_production;
    access_log /var/log/nginx/upstream.log upstream_log;
}

# 配置Prometheus exporter
docker run -d --name nginx-exporter \
    -p 9113:9113 \
    -e NGINX_STATUS_URL=http://127.0.0.1:8082/stub_status \
    nginx/nginx-prometheus-exporter

# Prometheus告警规则
cat > /etc/prometheus/rules/nginx_alerts.yml << 'EOF'
groups:
  - name: nginx
    rules:
      - alert: NginxUpstreamDown
        expr: nginx_upstream_down_count > 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Nginx上游服务器不可用"
          description: "{{ $value }} 台上游服务器处于down状态"
      
      - alert: NginxHighResponseTime
        expr: histogram_quantile(0.95, 
            rate(nginx_http_request_duration_seconds_bucket[5m])) > 2
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Nginx P95响应时间过高"
          description: "95分位响应时间超过2秒"
      
      - alert: NginxHighErrorRate
        expr: |
          sum(rate(nginx_http_requests_total{status=~"5.."}[5m])) /
          sum(rate(nginx_http_requests_total[5m])) > 0.05
        for: 2m
        labels:
          severity: critical
        annotations:
          summary: "Nginx 5xx错误率过高"
          description: "5xx错误率超过5%"
EOF

蓝绿部署与灰度发布配置

利用Nginx的upstream权重和split_clients模块实现蓝绿部署和灰度发布。蓝绿部署通过切换两套upstream实现零停机发布,灰度发布通过权重或变量控制流量比例。

# 蓝绿部署配置
upstream blue {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
}

upstream green {
    server 192.168.1.20:8080;
    server 192.168.1.21:8080;
}

map $cookie_deploy_version $upstream_pool {
    default blue;
    "v2"   green;
}

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://$upstream_pool;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# 灰度发布(按比例分流)
split_clients "${remote_addr}${http_user_agent}" $gray_upstream {
    10%  green;
    *    blue;
}

server {
    listen 80;
    server_name api.example.com;
    
    location / {
        proxy_pass http://$gray_upstream;
        proxy_set_header Host $host;
        proxy_set_header X-Gray-Version $gray_upstream;
    }
}

Nginx负载均衡配置需要根据实际业务场景调整参数。高并发场景关注连接复用和缓冲区大小,长连接场景关注WebSocket和超时配置,多机房场景关注健康检查和故障转移策略。定期审查upstream配置,清理下线服务器,更新权重分配,保持负载均衡配置与业务需求同步。健康检查模块的参数调优需要平衡检测灵敏度和误判率,过于敏感的检查会导致正常服务器被误剔除,过于宽松的检查会延迟故障发现。

原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/nginx-fu-zai-jun-heng-pei-zhi-yu-shang-you-fu-wu-qi-jian/

(0)
小编小编
上一篇 20小时前
下一篇 20小时前

相关推荐