服务器负载均衡是高可用集群架构的核心组件,通过将流量分发到多台后端服务器来提升系统吞吐量和容错能力。Nginx和HAProxy是两款主流的开源负载均衡软件,Nginx擅长HTTP七层负载均衡,HAProxy在TCP四层负载均衡和会话保持方面表现更优。实际生产环境中两者常配合使用。
Nginx负载均衡配置与调度算法
Nginx通过upstream模块实现负载均衡,支持轮询、权重轮询、IP哈希、最少连接数四种调度算法。以下是一个完整的HTTP负载均衡配置示例:
# /etc/nginx/conf.d/load_balancer.conf
upstream web_backend {
# 权重轮询,weight越大分配概率越高
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 backup;
# 连接保持,减少TCP握手开销
keepalive 32;
keepalive_timeout 60s;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://web_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;
# 连接超时设置
proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# 失败重试
proxy_next_upstream error timeout http_502 http_503;
proxy_next_upstream_tries 2;
}
# 健康检查端点
location /health {
access_log off;
return 200 "ok\n";
}
}
max_fails和fail_timeout参数实现被动健康检查:30秒内如果某后端服务器失败3次,Nginx会将其标记为不可用,30秒后重试。backup参数标记备用服务器,仅在主服务器全部不可用时启用。
Nginx主动健康检查模块配置
Nginx开源版默认只支持被动健康检查,主动健康检查需要使用nginx_upstream_check_module第三方模块或Nginx Plus商业版。编译安装主动检查模块:
# 下载并编译
cd /usr/local/src
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar xzf nginx-1.24.0.tar.gz
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
cd nginx-1.24.0
./configure --add-module=../nginx_upstream_check_module --with-http_ssl_module
make && make install
配置主动健康检查:
upstream web_backend {
server 192.168.1.10:8080;
server 192.168.1.11:8080;
server 192.168.1.12:8080;
# 每3秒检查一次,连续2次失败标记为down,连续2次成功恢复
check interval=3000 rise=2 fall=2 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 80;
# 查看后端服务器健康状态
location /upstream_status {
check_status;
access_log off;
allow 192.168.1.0/24;
deny all;
}
}
HAProxy TCP四层负载均衡配置
HAProxy在TCP代理场景下性能优异,常用于数据库代理、Redis代理等非HTTP协议的负载均衡。以下配置实现MySQL读写分离:
# /etc/haproxy/haproxy.cfg
global
log /dev/log local0
maxconn 4096
daemon
defaults
log global
mode tcp
option tcplog
timeout connect 5s
timeout client 30s
timeout server 30s
retries 3
# MySQL写节点(主库)
frontend mysql_write
bind *:3306
default_backend mysql_master
# MySQL读节点(从库)
frontend mysql_read
bind *:3307
default_backend mysql_slaves
backend mysql_master
mode tcp
option tcp-check
tcp-check connect
server master1 192.168.1.20:3306 check inter 2000 rise 2 fall 3
backend mysql_slaves
mode tcp
balance roundrobin
option tcp-check
tcp-check connect
server slave1 192.168.1.21:3306 check inter 2000 rise 2 fall 3
server slave2 192.168.1.22:3306 check inter 2000 rise 2 fall 3
# HAProxy统计页面
listen stats
bind *:8404
mode http
stats enable
stats uri /stats
stats auth admin:password123
HAProxy会话保持与负载均衡算法
HAProxy支持多种负载均衡算法:roundrobin(动态权重轮询)、static-rr(静态权重轮询)、leastconn(最少连接数)、source(源IP哈希)、uri(URI哈希)。对于有状态服务,使用source算法保证同一客户端IP的请求始终路由到同一后端服务器。
backend web_servers
balance source
hash-type consistent
option http-keep-alive
option forwardfor
cookie SERVERID insert indirect nocache
server web1 192.168.1.10:8080 cookie s1 check
server web2 192.168.1.11:8080 cookie s2 check
cookie参数实现基于Cookie的会话保持,HAProxy在响应中插入SERVERID Cookie,后续请求携带该Cookie即可路由到对应服务器。hash-type consistent使用一致性哈希算法,后端服务器增减时最小化会话迁移。
Nginx与HAProxy性能对比与选型
HTTP七层负载均衡选Nginx,配置灵活,社区生态丰富,事件驱动模型在处理大量并发HTTP请求时表现优异。TCP四层负载均衡选HAProxy,连接管理能力强,支持更细粒度的连接超时和重试控制。两者可以串联使用:HAProxy作为前端TCP负载均衡器处理SSL卸载和TCP分发,Nginx作为后端HTTP负载均衡器处理URL路由和内容缓存。
性能测试方面,在4核8GB虚拟机上,Nginx HTTP代理QPS约5-8万,HAProxy TCP代理连接数可达10万+。生产环境中Nginx建议worker_processes设置为CPU核数,worker_connections设置为10240;HAProxy建议maxconn设置为4096,并根据系统文件描述符限制调整ulimit。
原创文章,作者:小编,如若转载,请注明出处:https://www.yunthe.com/fu-wu-qi-fu-zai-jun-heng-shi-zhan-nginx-yu-haproxy-fan/