HAProxy 负载均衡器配置教程
HAProxy 是业界领先的开源负载均衡和反向代理软件,被 GitHub、Stack Overflow、Reddit 等大型网站广泛使用。它支持四层(TCP)和七层(HTTP)的负载均衡,能够处理数百万并发连接,具备完善的健康检查、会话保持和流量管理能力。本文将介绍如何在搬瓦工 VPS 上安装和配置 HAProxy,为你的 Web 应用提供专业的负载均衡方案。
一、安装 HAProxy
1.1 Ubuntu/Debian 安装
apt update
apt install haproxy -y
如果需要最新版本,可以添加官方 PPA:
apt install software-properties-common -y
add-apt-repository ppa:vbernat/haproxy-2.8 -y
apt update
apt install haproxy -y
1.2 CentOS 安装
yum install haproxy -y
验证安装:
haproxy -v
二、HAProxy 配置结构
HAProxy 的配置文件位于 /etc/haproxy/haproxy.cfg,由四个主要部分组成:
- global:全局参数,如日志、进程数、最大连接数等。
- defaults:所有代理段的默认参数。
- frontend:前端,定义如何接收客户端连接。
- backend:后端,定义实际处理请求的服务器组。
三、HTTP 七层负载均衡配置
cat > /etc/haproxy/haproxy.cfg <<EOF
global
log /dev/log local0
log /dev/log local1 notice
maxconn 50000
user haproxy
group haproxy
daemon
stats socket /run/haproxy/admin.sock mode 660 level admin
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
timeout connect 5s
timeout client 30s
timeout server 30s
retries 3
frontend http_front
bind *:80
default_backend web_servers
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/site.pem
http-request set-header X-Forwarded-Proto https
default_backend web_servers
backend web_servers
balance roundrobin
option httpchk GET /health
http-check expect status 200
server web1 192.168.1.10:8080 check inter 5s fall 3 rise 2
server web2 192.168.1.11:8080 check inter 5s fall 3 rise 2
server web3 192.168.1.12:8080 check inter 5s fall 3 rise 2 backup
EOF
四、TCP 四层负载均衡配置
四层负载均衡不解析应用层协议,适合 MySQL、Redis 等非 HTTP 服务:
frontend mysql_front
bind *:3306
mode tcp
default_backend mysql_servers
backend mysql_servers
mode tcp
balance leastconn
option tcp-check
server db1 192.168.1.20:3306 check inter 10s
server db2 192.168.1.21:3306 check inter 10s backup
五、负载均衡算法
- roundrobin:轮询(默认),按顺序将请求分配到各服务器。
- leastconn:最少连接数优先,适合长连接场景。
- source:基于客户端 IP 的哈希,确保同一 IP 访问同一后端。
- uri:基于 URI 的哈希,相同 URL 始终转发到同一服务器。
六、启用统计面板
frontend stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:your_password
stats admin if TRUE
访问 http://你的VPS_IP:8404/stats 即可查看实时负载状态、后端服务器健康状态和流量统计。
七、会话保持(Sticky Session)
backend web_servers
balance roundrobin
cookie SERVERID insert indirect nocache
server web1 192.168.1.10:8080 check cookie s1
server web2 192.168.1.11:8080 check cookie s2
通过 Cookie 实现会话保持,确保用户在一次会话中始终访问同一后端服务器。
八、SSL/TLS 终止
合并证书和私钥为 PEM 文件:
cat /etc/letsencrypt/live/yourdomain.com/fullchain.pem \
/etc/letsencrypt/live/yourdomain.com/privkey.pem \
> /etc/haproxy/certs/site.pem
配置 HTTP 到 HTTPS 的自动重定向:
frontend http_front
bind *:80
http-request redirect scheme https unless { ssl_fc }
九、启动与验证
haproxy -c -f /etc/haproxy/haproxy.cfg
systemctl restart haproxy
systemctl enable haproxy
systemctl status haproxy
十、常见问题
后端服务器全部显示 DOWN
检查健康检查配置,确认后端服务器的检查路径返回 200 状态码:
curl -v http://192.168.1.10:8080/health
配置文件语法错误
使用 HAProxy 的检查功能验证配置:
haproxy -c -f /etc/haproxy/haproxy.cfg
总结
HAProxy 是最成熟可靠的负载均衡方案,无论是小型项目还是大流量网站都能胜任。在搬瓦工 VPS 上部署 HAProxy 可以为多台后端服务器提供统一的入口和流量分配。配合 Keepalived 高可用配置,可以进一步消除单点故障风险。购买搬瓦工 VPS 请查看 全部方案,使用优惠码 NODESEEK2026 可享 6.77% 折扣,购买链接:bwh81.net。