隨著流量增長,單點伺服器無法承載。HAProxy 以單執行緒事件驅動架構、極高效能(輕鬆跑滿 10Gbps)、豐富功能(健康檢查、多種演算法、SSL 卸載、速率限制)成為開源負載均衡首選。本文手把手教您在 Linux 上部署 HAProxy 實現 HTTP 負載均衡,含日誌、統計頁、後端叢集、會話保持完整配置。
| 發行版 | 指令 |
|---|---|
| Debian 11/12 | sudo apt update && sudo apt install haproxy |
| Ubuntu 20.04/22.04/24.04 | sudo apt update && sudo apt install haproxy |
| CentOS 7 / RHEL 7 | sudo yum install haproxy(或啟用 EPEL) |
| CentOS 8/9 / Rocky / AlmaLinux | sudo dnf install haproxy |
| Fedora | sudo dnf install haproxy |
| Arch | sudo pacman -S haproxy |
global
log 127.0.0.1 local0
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
maxconn 20000
defaults
log global
mode http
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout connect 5s
timeout client 50s
timeout server 50s
timeout http-request 10s
timeout http-keep-alive 5s
timeout check 5s
maxconn 20000
# 前端:接收客戶端流量
frontend http_front
bind *:80
# 可選:重導向 HTTP → HTTPS
# redirect scheme https code 301 if !{ ssl_fc }
default_backend web_farm
# 後端:Web 伺服器叢集
backend web_farm
balance roundrobin
# 會話保持:插入 Cookie,間接模式,不快取
cookie LBN insert indirect nocache
option httpchk GET /healthz
http-check expect status 200
server web01 192.168.100.2:80 cookie node1 check inter 2s fall 3 rise 2
server web02 192.168.100.3:80 cookie node2 check inter 2s fall 3 rise 2
# 統計頁面(建議限制存取 IP 或加上基本認證)
listen stats
bind *:8404
stats enable
stats uri /haproxy?stats
stats realm HAProxy\ Statistics
stats auth admin:strongpassword
stats refresh 10s
| 指令 | 說明 |
|---|---|
balance roundrobin | 輪詢演算法;可改 leastconn(最少連線)、source(IP 雜湊)、uri、url_param 等 |
cookie LBN insert indirect nocache | HAProxy 插入 Cookie 名為 LBN,間接模式(不修改後端回應),不快取 |
option httpchk GET /healthz | 健康檢查用 HTTP GET /healthz,回 200 視為健康 |
check inter 2s fall 3 rise 2 | 每 2 秒檢查,連續 3 次失敗標記下線,連續 2 次成功標記上線 |
option forwardfor | 在請求頭插入 X-Forwarded-For,後端可獲取真實客戶端 IP |
option http-server-close | HTTP/1.1 keep-alive 但服務端主動關閉,減少連線佔用 |
# /etc/rsyslog.d/haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
local0.* ~
# 重啟 rsyslog
sudo systemctl restart rsyslog
同步修改 haproxy.cfg global 區:log 127.0.0.1 local0
# /etc/rsyslog.d/49-haproxy.conf
$ModLoad imudp
$UDPServerRun 514
$template Haproxy,"%msg%\n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
local0.* ~
sudo systemctl restart rsyslog
修改 haproxy.cfg:log 127.0.0.1 local0(預設可能為 /dev/log)。
# 語法檢查
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# 啟動/重啟
sudo systemctl enable --now haproxy
sudo systemctl restart haproxy
# 檢查狀態
sudo systemctl status haproxy
ss -ltnp | grep haproxy
# 多次請求觀察分發
for i in {1..10}; do curl -s https://192.168.100.4/ | grep "Server IP"; done
# 查看統計頁
# 瀏覽器開啟 https://192.168.100.4:8404/haproxy?stats
# 帳號:admin / 密碼:strongpassword
# 合併憑證與私鑰
cat server.crt server.key > /etc/haproxy/certs/server.pem
# frontend 新增
frontend https_front
bind *:443 ssl crt /etc/haproxy/certs/server.pem alpn h2,http/1.1
http-request set-header X-Forwarded-Proto https
default_backend web_farm
# 並將 http_front 加上重導向
frontend http_front
bind *:80
redirect scheme https code 301 if !{ ssl_fc }
default_backend web_farm
單點 HAProxy 仍是單點故障。生產環境建議部署雙機熱備:
keepalived,設定 VRRP 虛擬 IP (VIP)maxconn:依記憶體與檔案描述符上限調大(ulimit -n)nbproc / nbthread:多核心可啟用多進程/執行緒(需 HAProxy 2.0+)tune.bufsize、tune.maxrewrite:大請求/回應調大緩衝區cpu-map 綁定 CPU 核心減少上下文切換ssl-engine 硬體加速 SSL(如 Intel QAT、AWS Nitro)HAProxy 配置看似繁多,但核心邏輯清晰:Frontend 接流量 → Backend 分流量 → Server 幹活。掌握健康檢查、會話保持、統計監控三大支柱,再配合 Keepalived 實現高可用,即可構建企業級 HTTP 負載均衡基礎設施。
AIX 系統斷電後 HACMP 無法啟動:磁碟識別錯亂完整排查與修復實錄
Linux pppsetup 指令介紹:Slackware 內建 PPP 連線互動式設定工具
Linux eCryptFS 文件加密實戰:從安裝到掛載的完整指南
Linux 自動調整螢幕亮度方案:Calise 攝像頭環境光感應 + 手動指令備選
修復 Linux 下 gulp watch ENOSPC 錯誤:調大 inotify 監控上限
Linux vi/vim 編輯器卡死於 connect(/dev/gpmctl) 的解決方法
紅旗 Linux 6.0 安裝 VirtualBox 完整教學:從下載到建立虛擬機
Ubuntu USB 開機報錯 "BOOT ERROR" 解決方案:BIOS USB 模式 (ZIP/HDD/Auto) 與啟動順序調整實戰