1. 基础负载均衡配置
在nginx.conf
的http
块中添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| http { upstream backend { server 192.168.1.101:8080; server 192.168.1.102:8080; server 192.168.1.103:8080; }
server { listen 80; location / { proxy_pass http://backend; proxy_set_header Host $host; } } }
|
2. 负载均衡策略详解
2.1 加权轮询(Weighted Round Robin)
1 2 3 4 5
| upstream backend { server 192.168.1.101:8080 weight=5; server 192.168.1.102:8080 weight=3; server 192.168.1.103:8080 weight=2; }
|
2.2 最少连接(Least Connections)
1 2 3 4 5
| upstream backend { least_conn; server 192.168.1.101:8080; server 192.168.1.102:8080; }
|
2.3 IP哈希(Session Persistence)
1 2 3 4 5
| upstream backend { ip_hash; server 192.168.1.101:8080; server 192.168.1.102:8080; }
|
2.4 响应时间动态分配(商业版功能)
1 2 3 4 5
| upstream backend { fair; server 192.168.1.101:8080; server 192.168.1.102:8080; }
|
3. 高级配置参数
3.1 健康检查机制
1 2 3 4
| upstream backend { server 192.168.1.101:8080 max_fails=3 fail_timeout=30s; server 192.168.1.102:8080 max_fails=3 fail_timeout=30s; }
|
max_fails
:允许的最大失败次数
fail_timeout
:服务器被标记为不可用的时间
3.2 备份服务器配置
1 2 3 4
| upstream backend { server 192.168.1.101:8080; server 192.168.1.102:8080 backup; }
|
3.3 慢启动机制
1 2 3
| upstream backend { server 192.168.1.101:8080 slow_start=30s; }
|
4. 完整配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| http { upstream myapp { least_conn; server app1.example.com:8080 weight=3; server app2.example.com:8080 max_fails=2; server backup.example.com:8080 backup; }
server { listen 80; server_name loadbalancer.example.com;
location /static/ { root /var/www/html; expires 30d; }
location / { proxy_pass http://myapp; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; proxy_connect_timeout 5s; }
location /nginx_status { stub_status on; access_log off; allow 192.168.1.0/24; deny all; } } }
|
5. 监控与调试
5.1 查看实时状态
1
| curl http://localhost/nginx_status
|
1 2 3 4
| Active connections: 3 server accepts handled requests 12 12 24 Reading: 0 Writing: 1 Waiting: 2
|
5.2 日志分析配置
1 2 3 4 5 6
| log_format upstream_log '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' 'upstream: $upstream_addr response_time: $upstream_response_time';
access_log /var/log/nginx/access.log upstream_log;
|
6. 性能优化建议
1.连接复用配置:
1 2 3
| proxy_http_version 1.1; proxy_set_header Connection ""; keepalive 32;
|
2.缓冲区优化:
1 2
| proxy_buffers 16 32k; proxy_buffer_size 64k;
|
3.超时控制:
1 2 3
| proxy_connect_timeout 5s; proxy_send_timeout 10s; proxy_read_timeout 15s;
|
7. 常见问题排查
7.1 502 Bad Gateway
检查项:
1 2
| ss -tnlp | grep 8080 tail -f /var/log/nginx/error.log
|
7.2 流量分配不均
- 确认是否启用
ip_hash
导致固定分配
- 检查后端服务器的响应时间差异
8. TLS终止配置
1 2 3 4 5 6 7 8 9 10
| server { listen 443 ssl; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; location / { proxy_pass http://backend; proxy_set_header X-Forwarded-Proto https; } }
|
配置完成后执行:
1
| sudo nginx -t && sudo systemctl reload nginx
|
根据实际业务需求选择合适的策略组合,建议先进行压力测试(可使用ab
或wrk
工具)验证配置效果。