本文首發於我的個人網站: https://hewanyue.com/
本文作者: Hechao
本文鏈接: https://hewanyue.com/blog/f5daec50.html
haproxy的進階配置
haproxy報文修改
在http模式下,基於實際需求修改客戶端的請求報文與響應報文,通過reqadd和reqdel在請求報文添加刪除字段,通過rspadd與rspidel在響應報文中添加與刪除字段。
在請求報文尾部添加指定首部
reqadd <string> [{if | unless} <cond>]
從請求報文中刪除匹配正則表達式的首部
reqdel <search> [{if | unless} <cond>]
reqidel <search> [{if | unless} <cond>]
在響應報文尾部添加指定首部
rspadd <string> [{if | unless} <cond>]
示例:
rspadd X-Via:\ HAPorxy
從響應報文中刪除匹配正則表達式的首部
rspdel <search> [{if | unless} <cond>]
rspidel <search> [{if | unless} <cond>]
示例:
rspidel server.* #從相應報文刪除server信息
rspidel X-Powered-By:.* #從響應報文刪除X-Powered-By信息
HAProxy日誌
HAProxy日誌配置
需在HAProxy和Rsyslog中分別配置。
- HAProxy配置
在global配置項定義:
log 127.0.0.1 local{1-7} info #基於syslog記錄日誌到指定設備,級別有(err、warning、info、debug)
listen web_port
bind 127.0.0.1:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
然後重啓HAProxysystemctl restart haproxy
- Rsyslog配置
編輯配置文件vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local3.* /var/log/haproxy.log
然後重啓Rsyslogsystemctl restart rsyslog
HAProxy日誌格式
將特定信息記錄在日誌中
配置選項:
capture cookie <name> len <length>
#捕獲請求和響應報文中的 cookie並記錄日誌
capture request header <name> len <length>
#捕獲請求報文中指定的首部內容和長度並記錄日誌
capture response header <name> len <length>
#捕獲響應報文中指定的內容和長度首部並記錄日誌
示例:
capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
配置示例
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog #日誌格式選項
capture request header X-Forwarded-For len 15
capture request header User-Agent len 512
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.32.81:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.32.82:80 cookie web2 check inter 3000 fall 3 rise 5
壓縮功能
啓用功能可以對響應給客戶端的報文進行壓縮,以節省網絡帶寬,但是會佔用部分CPU性能。
配置選項
compression algo #啓用http協議中的壓縮機制,常用算法有gzip deflate
identity #調試使用的壓縮方式
gzip #常用的壓縮方式,與各瀏覽器兼容較好
deflate #有些瀏覽器不支持
raw-deflate #新出的壓縮方式
compression type #要壓縮的文件類型
配置示例
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
# capture request header X-Forwarded-For len 15
# capture request header User-Agent len 512
compression algo gzip
compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.32.81:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.32.82:80 cookie web2 check inter 3000 fall 3 rise 5
後端服務器狀態監測
haproxy能對後端服務器狀態進行檢測,如果發現後端服務器異常,可以自動將該服務器下線,實現高可用。
haproxy對後端服務器有三種檢測方式:
- 基於四層的傳輸端口做狀態監測
- 基於指定URI 做狀態監測
- 基於指定URI的request請求頭部內容做狀態監測
option httpchk
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>
之前我們的配置都是基於傳輸IP加端口對檢測,所以status狀態頁的後端檢測狀態裏顯示的是L4,基於指定URI做狀態監測,需要持續從服務器get指定頁面,會佔用消耗一些帶寬資源,所以基於指定URI的request請求頭部內容做狀態監測最爲合理,配置如下:
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
# option httpchk GET /app/monitor/check.html HTTP/1.0
option httpchk HEAD /app/monitor/check.html HTTP/1.0\r\nHost:\ 192.168.7.102
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.32.81:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.32.82:80 cookie web2 check inter 3000 fall 3 rise 5
這時再去看9999端口的status頁,就會看到後端服務器的檢測狀態爲L7OK了
HAProxy的ACL功能
訪問控制列表(ACL,Access Control Lists)是一種基於包過濾的訪問控制技術,它可以根據設定的條件對經過服務器傳輸的數據包進行過濾(條件匹配),即對接收到的報文進行匹配和過濾,基於請求報文頭部中的源地址、源端口、目標地址、目標端口、請求方法、URL、文件後綴等信息內容進行匹配並執行進一步操作,允許其通過或丟棄。
ACL配置選項:
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名稱 匹配規範 匹配模式 具體操作符 操作對象類型
ACL-Name
實例:acl image_service hdr_dom(host) -i img.example.com
ACL名稱,可以使用大字母A-Z、小寫字母a-z、數字0-9、冒號:、點.、中橫線和下劃線,並且嚴格區分大小寫,比如Image_site和image_site完全是兩個acl。
ACL-criterion
定義ACL匹配規範
hdr([<name> [,<occ>]]):完全匹配字符串
hdr_beg([<name> [,<occ>]]):前綴匹配
hdr_dir([<name> [,<occ>]]):路徑匹配
hdr_dom([<name> [,<occ>]]):域匹配
hdr_end([<name> [,<occ>]]):後綴匹配
hdr_len([<name> [,<occ>]]):長度匹配
hdr_reg([<name> [,<occ>]]):正則表達式匹配
hdr_sub([<name> [,<occ>]]):子串匹配
dst 目標IP
dst_port 目標PORT
src 源IP
src_port 源PORT
示例:
hdr <string>用於測試請求頭部首部指定內容
hdr_dom(host) 請求的host名稱,如 www.example.com
hdr_beg(host) 請求的host開頭,如 www. img. video. download. ftp.
hdr_end(host) 請求的host結尾,如 .com .net .cn
path_beg 請求的URL開頭,如/static、/images、/img、/css
path_end 請求的URL中資源的結尾,如 .gif .png .css .js .jpg .jpeg
ACL-flags
ACL匹配模式
-i 不區分大小寫
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否則多個同名ACL匹配或關係
ACL-operator
ACL 操作符
整數比較:eq、ge、gt、le、lt
字符比較:
exact match (-m str) :字符串必須完全匹配模式
substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一個被發現,ACL將匹配
prefix match (-m beg) :在提取的字符串首部中查找模式,如果其中任何一個被發現,ACL將匹配
suffix match (-m end) :將模式與提取字符串的尾部進行比較,如果其中任何一個匹配,則ACL進行匹配
subdir match (-m dir) :查看提取出來的用斜線分隔(“/”)的字符串,如果其中任何一個匹配,則ACL進行匹配
domain match (-m dom) :查找提取的用點(“.”)分隔字符串,如果其中任何一個匹配,則ACL進行匹配
ACL-value
value的類型
The ACL engine can match these types against patterns of the following types :
Boolean #布爾值
integer or integer range #整數或整數範圍,比如用於匹配端口範圍
IP address / network #IP地址或IP範圍, 192.168.0.1 ,192.168.0.1/24
string
exact –精確比較
substring—子串 www.example.com
suffix-後綴比較
prefix-前綴比較
subdir-路徑, /wp-includes/js/jquery/jquery.js
domain-域名,www.example.com
regular expression #正則表達式
hex block #16進制
ACL調用方式
ACL調用方式:
- 與:隱式(默認)使用
- 或:使用“or” 或 “||”表示
- 否定:使用“!“ 表示
示例:
if valid_src valid_port #與關係
if invalid_src || invalid_port #或
if ! invalid_src #非
ACL具體示例
域名匹配:
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl web_host hdr_dom(host) www.example.net
use_backend example_host if web_host
default_backend default_web
backend example_host
mode http
server web1 192.168.32.81:80 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web2 192.168.32.82:80 check inter 2000 fall 3 rise 5
匹配瀏覽器類型:
匹配客戶端瀏覽器,將不同類型的瀏覽器調動至不同的服務器組
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl web_host hdr_dom(host) www.example.net
use_backend example_host if web_host
acl redirect_test hdr(User-Agent) -m sub -i "Mozilla/5.0 (Windows NT 6.1; WOW64;Trident/7.0; rv:11.0) like Gecko"
redirect prefix http://192.168.7.103 if redirect_test
default_backend default_web
backend example_host
mode http
server web1 192.168.32.81:80 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web2 192.168.32.82:80 check inter 2000 fall 3 rise 5
基於文件後綴名實現動靜分離:
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl php_server path_end -i .php
use_backend php_server_host if php_server
acl image_server path_end -i .jpg .png .jpeg .gif
use_backend image_server_host if image_server
default_backend default_web
backend php_server_host
mode http
server web1 192.168.32.81 check inter 2000 fall 3 rise 5
backend image_server_host
mode http
server web1 192.168.32.82 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 192.168.32.8:80 check inter 2000 fall 3 rise 5
匹配訪問路徑實現動靜分離:
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl static_path path_beg -i /static /images /javascript
use_backend static_path_host if static_path
default_backend default_web
backend static_path_host
mode http
server web1 192.168.32.81 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 192.168.32.8:80 check inter 2000 fall 3 rise 5
預定義ACL
預定義ACL:
ACLname | Equivalent to | Usage |
---|---|---|
FALSE | always_false | never match |
HTTP | req_proto_http | match if protocol is valid HTTP |
HTTP_1.0 | req_ver 1.0 | match HTTP version 1.0 |
HTTP_1.1 | req_ver 1.1 | match HTTP version 1.1 |
HTTP_CONTENT | hdr_val(content-length) gt 0 | match an existing content-length |
HTTP_URL_ABS | url_reg [/:]*: // | match absolute URL with scheme |
HTTP_URL_SLASH | url_beg / | match URL beginning with “/” |
HTTP_URL_STAR | url * | match URL equal to “*” |
LOCALHOST | src 127.0.0.1/8 | match connection from local host |
METH_CONNECT | method CONNECT | match HTTP CONNECT method |
METH_DELETE | method DELETE | match HTTP DELETE method |
METH_GET | method GET HEAD | match HTTP GET or HEAD method |
METH_HEAD | method HEAD | match HTTP HEAD method |
METH_OPTIONS | method OPTIONS | match HTTP OPTIONS method |
METH_POST | method POST | match HTTP POST method |
METH_PUT | method PUT | match HTTP PUT method |
METH_TRACE | method TRACE match | HTTP TRACE method |
RDP_COOKIE | req_rdp_cookie_cnt gt 0 | match presence of an RDP cookie |
REQ_CONTENT | req_len gt 0 | match data in the request buffer |
TRUE | always_true | always match |
WAIT_END | wait_end | wait for end of content analysis |
預定義ACL使用示例
listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl static_path path_beg -i /static /images /javascript
use_backend static_path_host if HTTP_1.1 TRUE static_path
default_backend default_web
backend php_server_host
mode http
server web1 192.168.32.81 check inter 2000 fall 3 rise 5
backend static_path_host
mode http
server web1 192.168.32.82 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 192.168.32.8:80 check inter 2000 fall 3 rise 5
詳細信息查看官網http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7.4