首先是haproxy.cfg文件的基本標注
當然實際配件沒有下面這個復雜,可以根據需要自行增減。
global
log 127.0.0.1 local1
maxconn 65000 #最大連接數
chroot /usr/local/haproxy #安裝目錄
uid 99 #用戶haproxy
gid 99 #組haproxy
daemon #守護進程運行
nbproc 1 #進程數量
pidfile /usr/local/haproxy/logs/haproxy.pid #haproxy pid
defaults
log global
mode http #7層 http;4層tcp
option httplog #http 日志格式
option httpclose #主動關閉http通道
option redispatch #serverId對應的服務器掛掉后,強制定向到其他健康的服務器
option forwardfor
option dontlognull
maxconn 50000 #最大連接數
contimeout 5000 #連接超時(毫秒)
clitimeout 50000 #客戶端超時(毫秒)
srvtimeout 50000 #服務器超時(毫秒)
#errorfile 502 /usr/local/haproxy/html/maintain.html
#errorfile 503 /usr/local/haproxy/html/maintain.html
#errorfile 504 /usr/local/haproxy/html/maintain.html
frontend test.com #定義前端服務器(haproxy)
bind *:80 #監聽地址
acl web-client path_beg -i /vsphere-client
acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum)
acl monitor hdr_beg(host) -i monitor.test.com #定義ACL名稱,對應的請求的主機頭是monitor.test.com
acl www hdr_beg(host) -i www.test.com
use_backend cache.test.com if static
use_backend monitor.test.com if bbs or monitor
use_backend www.test.com if www
use_backend vsphere-client if web-client
default_backend www.test.com #指定默認的后端服務器
backend monitor.test.com #定義后端服務器群(web server/apache/nginx/iis..)
mode http
option forwardfor #后端服務器(apache/nginx/iis/*),從Http Header中獲得客戶端IP
balance leastconn #負載均衡的方式,最小連接
cookie SERVERID #插入serverid到cookie中,serverid后面可以定義
option httpchk HEAD /check.html #用來做健康檢查html文檔
#option httpchk HEAD /index.php HTTP/1.1\r\nHost:monitor.test.com #HTTP && Host
server server1 10.0.100.70:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
#服務器定義:
#cookie server1表示serverid為server1;
#check inter 2000 是檢測心跳頻率(check 默認 );
#rise 3 表示 3次正確認為服務器可用;
#fall 3 表示 3次失敗認為服務器不可用;
#weight 表示權重。
backend www.test.com
mode http
option forwardfor
balance roundrobin #負載均衡的方式,輪詢方式
cookie SERVERID
option httpchk HEAD /check.html
server server1 10.0.100.71:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3
backend vsphere-client
mode http
option forwardfor header ORIG_CLIENT_IP
balance roundrobin
server server1 10.0.100.81:80 redir https://192.168.57.81:443 check inter 2000 rise 3 fall 3 weight 3
backend cache.test.com
option forwardfor
#balance uri len 15 #url hash
balance roundrobin
server server1 10.0.100.73:80 check inter 2000 rise 3 fall 3 weight 3
server server2 10.0.100.75:80 check inter 2000 rise 3 fall 3 weight 3
listen admin_stat #status
bind 0.0.0.0:8080 #監聽端口
mode http #http的7層模式
stats refresh 30s #統計頁面自動刷新時間
stats uri /haproxy_stats_url #統計頁面URL
stats realm Haproxy\ Statistics #統計頁面密碼框上提示文本
stats auth admin:admin #統計頁面用戶名和密碼設置
stats hide-version #隱藏統計頁面上HAProxy的版本信息
stats admin if TRUE #手工啟用/禁用,后端服務器
下面重點介紹一下ACL策略,這個匹配策略很重要。
下面是一個根據域名匹配的寫法
acl a-server base_dom -i www.a.com
use_backend a-server if a-server
acl b-server base_dom -i www.b.com
use_backend b-server if b-server
##以下表示請求在以上路徑都不符合時,使用該后台服務器處理
default_backend root
acl語法
#acl 參數
acl(關鍵字) 定義acl(名稱) 方法(criterion) -i (flags) [匹配的路徑或文件]
hdr_beg(host)
hdr_reg(host)
path_beg
path_end
criterion
#base
base : exact string match
base_beg : prefix match
base_dir : subdir match
base_dom : domain match
base_end : suffix match
base_len : length match
base_reg : regex match
base_sub : substring match
---------------------
#path
path : exact string match
path_beg : prefix match
path_dir : subdir match
path_dom : domain match
path_end : suffix match
path_len : length match
path_reg : regex match
path_sub : substring match
---------------------
#url
url : exact string match
url_beg : prefix match
url_dir : subdir match
url_dom : domain match
url_end : suffix match
url_len : length match
url_reg : regex match
url_sub : substring match
---------------------
#hdr
hdr([<name>[,<occ>]]) : exact string match
hdr_beg([<name>[,<occ>]]) : prefix match hdr_dir([<name>[,<occ>]]) : subdir match hdr_dom([<name>[,<occ>]]) : domain match hdr_end([<name>[,<occ>]]) : suffix match hdr_len([<name>[,<occ>]]) : length match hdr_reg([<name>[,<occ>]]) : regex match hdr_sub([<name>[,<occ>]]) : substring match ---------------------
flags
-i 不區分大小寫
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 強制每個ACL必須唯一ID,否則多個同名ACL或關系
– 強制flag結束. 當字符串和某個flag相似時使用
參考:
https://blog.csdn.net/nange_nice/article/details/78444793
https://blog.51cto.com/dngood/886547