編譯自:
using ACLs and pattern extraction
文檔版本:HAProxy version 1.4.27
目錄:
- 整數(shù)匹配
- 字符串匹配
- 正則匹配
- IPv4地址匹配
- 可用的測試區(qū)域
- 4層及以下的匹配
- 4層匹配
- 7層匹配
- 預(yù)定義的 ACLs
- 結(jié)合條件判斷使用 ACLs
- 模式提取
- 動靜分離示例(不屬于原文,馬哥課程給出的示例)
使用 ACLs,可以基于請求和響應(yīng)的任何部分,進(jìn)行服務(wù)內(nèi)容的切換??偟脑瓌t如下:
- 定義測試準(zhǔn)則
- 根據(jù)測試結(jié)果執(zhí)行一定的動作,包括對請求進(jìn)行拒絕,或者選擇一個 backend
acl 關(guān)鍵字用于定義一條新的測試準(zhǔn)則,或者對一條已經(jīng)存在的測試準(zhǔn)則進(jìn)行重新定義:
acl <aclname> <criterion> [flags] [operator] <value> ...
<criterion>:
指定對請求或者響應(yīng)的某個部分進(jìn)行測試
[flags]:
對測試進(jìn)行調(diào)整
-i: 此 flag 后續(xù)的 patterns 進(jìn)行模式匹配時忽略大小寫,之前的 patterns 不受影響
例如:
acl valid-ua hdr(user-agent) -f exact-ua.lst -i -f generic-ua.lst test
exact-ua.lst 中的 pattern 對大小寫敏感
generic-ua.lst 中的 pattern 忽略大小寫
test 字符串忽略大小寫
-f: 從文件中加載 pattern
可使用多個 -f 選項(xiàng)指定多個文件。
如果一個 pattern 的解析錯誤,1.4 版無法準(zhǔn)確定位到錯誤的 pattern,只能大概知道 ACL
有一個解析錯誤。
文件中的 patterns 可能被加載到二叉樹中,因此查找速度很快。
--: 強(qiáng)制終止 flags。當(dāng)有字符串看起來與 flags 類似時可以使用 -- 顯式地終止 flags。
[operator]:
某些準(zhǔn)則支持使用操作符
<value>:
指定測試準(zhǔn)則所支持的類型,多個 <value> 以空格分隔
支持的 <value> 類型包括:
- 整數(shù)、整數(shù)范圍
- 字符串
- 正則表達(dá)式
- IP地址、網(wǎng)絡(luò)地址
注,ACL名稱只能使用大寫字母、小寫字母、數(shù)字、-(中線)、_(下劃線)、.(點(diǎn)號)和:(冒號)。此外,ACL名稱會區(qū)分字母大小寫。my_acl 和 My_Acl 是兩個不同的 ACLs。
對 ACLs 的數(shù)量沒有限制,它們只占用少量內(nèi)存。
7.1 整數(shù)匹配
整數(shù)匹配的規(guī)則:
1. 允許范圍匹配
例如:
1024:65535
1024:
0:1023
:1023
2. 支持對版本號進(jìn)行檢測:1.2
3. 允許使用比較運(yùn)算符,與 bash 類似:
eq: 等于
ge: 大于等于
gt: 大于
le: 小于等于
小于: 小于
示例:
acl negative-length hdr_val(content-length) lt 0 (內(nèi)容長度小于0)
acl sslv3 req_ssl_ver 3:3.1 (SSL 版本為 3.0~3.1 之間)
4. 只對正數(shù)進(jìn)行匹配
7.2 字符串匹配
這是指準(zhǔn)確的字符串匹配,有一些規(guī)則如下:
\ 可對空格等特殊字符進(jìn)行轉(zhuǎn)義,轉(zhuǎn)為普通字符
-i string,表示忽略大小寫
-
匹配特殊字符串如 -i, --,
方法一:字符串前面加上 --,這時 flag 終止標(biāo)志
-- -i -- --方法二:把特殊字符串放第二個位置
str1 -i str1 --
7.3 正則匹配
基本規(guī)則同上
7.4 IPv4地址匹配
匹配某個IP地址:
192.168.0.110
www.guli.com(支持使用主機(jī)名,但不推薦這樣做,因?yàn)閷ε渲脕碇v,會造成閱讀和調(diào)試上的困難)
如果使用主機(jī)名,需確保 /etc/hosts 中有對應(yīng)的解析條目,不要依賴 DNS 解析。
匹配某個網(wǎng)絡(luò):
192.168.0.0/24
7.5 可用的測試區(qū)域
7.5.1 四層及以下的匹配
這是第一部分的測試區(qū)域,不需要對請求或響應(yīng)的內(nèi)容進(jìn)行分析。其中包含 TCP/IP 地址和端口,以及一些與流量無關(guān)的內(nèi)部信息。
always_false
這個永遠(yuǎn)不會匹配。所有的 values 和 flags 被忽略??稍谡{(diào)整配置時使用。
always_true
這個永遠(yuǎn)匹配。所有的 values 和 flags 被忽略??稍谡{(diào)整配置時使用。
avg_queue <integer>
avg_queue(backend) <integer>
某個指定 backend 的等待隊(duì)列長度的平均數(shù),如果在指定范圍內(nèi),返回 ture。
返回值為:某個 backend 的等待連接總數(shù) / 該 backend 活動的后端服務(wù)器總數(shù)。
根據(jù)該值,可大致判斷一個新的連接需要等待多久才會被處理。
其主要使用場景是,當(dāng)某個新的用戶確定只能得到降級的服務(wù),返回一個 sorry page 給該用戶。
應(yīng)注意的是,當(dāng) backend 中沒有活動的服務(wù)器了,我們會把等待隊(duì)列的連接數(shù)x2,作為測量值。這是一個公平的預(yù)測,因?yàn)槲覀兤谕幸慌_服務(wù)器能快速歸位。但這種情況下,更好的方法是將流量轉(zhuǎn)發(fā)給另一個后端。
be_conn <integer>
be_conn(backend) <integer>
返回指定 backend 中當(dāng)前已建立的連接數(shù)(possibly including the connection being evaluated),如果在指定范圍內(nèi),返回 ture。
如果沒有特別指定是那個 backend,則表示使用當(dāng)前的 backend。
可用于這樣的場景,如果被測試的 backend 中連接數(shù)已經(jīng)滿負(fù)荷,將其流量轉(zhuǎn)發(fā)給另一個 backend。
be_id <integer>
返回 backend 的 id。可在 frontend 中使用,用于檢測是被哪一個 backend 所調(diào)用。
be_sess_rate <integer>
be_sess_rate(backend) <integer>
返回“會話創(chuàng)建速率”:new sessions/s,如果在指定范圍內(nèi),返回 true。
使用場景:
1. 當(dāng)此值過高時,可將其流量轉(zhuǎn)發(fā)給其他 backend。
2. 限制“abuse of service”,比如:prevent sucking of an online dictionary;
示例:
# Redirect to an error page if the dictionary is requested too often
backend dynamic
mode http
acl being_scanned be_sess_rate gt 100
redirect location /denied.html if being_scanned
當(dāng)“會話創(chuàng)建速率” 高于 100 session/s,將請求重定向至一個 denied 頁面。
connslots <integer>
connslots(backend) <integer>
connslots = backend 服務(wù)器剩余的可容納的連接數(shù) + backend 的等待隊(duì)列中剩余的可容納的連接數(shù),在指定范圍內(nèi)時返回 true。
簡單來說,就是衡量一個 backend 當(dāng)前還能接收多少新建連接?;谶@個值,可以做更好的負(fù)載均衡。一般配合 fe_conn(frontend 當(dāng)前已經(jīng)接收的連接數(shù)) 一起做判斷。
dst <ip_address>
返回當(dāng)前客戶端所連接的本地的 IPv4 地址。It can be used to
switch to a different backend for some alternative addresses.
dst_conn <integer>
返回一個socket 上已經(jīng)建立的連接數(shù)(including the one being evaluated)。如果當(dāng)前接收的連接數(shù)已經(jīng)到達(dá)滿負(fù)荷,可用于在 hard-blocking 之前返回 sorry 頁面;或者,使用某個指定的 backend 接收新的請求。
我們可以使用它對不同的監(jiān)聽地址設(shè)置不同的限制。
dst_port <integer>
返回當(dāng)前客戶端所連接的本地的端口地址。It can be used to switch
to a different backend for some alternative ports.
fe_conn <integer>
fe_conn(frontend) <integer>
返回某個 frontend 中已經(jīng)建立的連接數(shù)(possibly including the connection being evaluated),如果在指定范圍內(nèi),返回 true。
如果未指定是哪個 frontend,使用當(dāng)前的 frontend。
如果所關(guān)聯(lián)的 backend 接收的連接數(shù)被認(rèn)為已經(jīng)到達(dá)滿負(fù)荷,可用于在 hard-blocking 之前返回 sorry 頁面;或者,使用某個指定的 backend 接收新的請求。
fe_id <integer>
返回 frontend 的 id。在 backend 中可使用它判斷自己是被哪個 frontend 所調(diào)用。
fe_sess_rate <integer>
fe_sess_rate(frontend) <integer>
返回 frontend 的 “新建會話速率”。單位為:新建會話數(shù)/秒。如果在指定范圍內(nèi),返回 true。
一般使用場景:限制新建連接的速率為一個可接受的范圍內(nèi)。在第一時間防止服務(wù)濫用。防止 ddos 攻擊等。
結(jié)合 4層 ACLs,可強(qiáng)制讓一個客戶端等待少許時間,等待新建會話率降至合理范圍內(nèi)。
示例:
# This frontend limits incoming mails to 10/s with a max of 100
# concurrent connections. We accept any connection below 10/s, and
# force excess clients to wait for 100 ms. Since clients are limited to
# 100 max, there cannot be more than 10 incoming mails per second.
frontend mail
bind :25
mode tcp
maxconn 100
acl too_fast fe_sess_rate ge 10
tcp-request inspect-delay 100ms
tcp-request content accept if ! too_fast
tcp-request content accept if WAIT_END
限制接收郵件速率為 10/s
最大并發(fā)連接數(shù)為 100
新建會話速率限制為:< 10/s。如果超過此限制,強(qiáng)制客戶端等待 100ms。
因?yàn)椴l(fā)客戶端數(shù)量被限制為 100,所以新郵件速率不可能大于 10/s
nbsrv <integer>
nbsrv(backend) <integer>
返回當(dāng)前 backend 或某個指定的 backend 的可用服務(wù)器數(shù)。當(dāng)可用服務(wù)器數(shù)過低時,可切換到其他 backend。
一般和 monitor fail 配合使用。
queue <integer>
queue(backend) <integer>
返回當(dāng)前 backend 或某個指定的 backend 的等待隊(duì)列中的連接數(shù)。
當(dāng)隊(duì)列長度超過警戒值,一般意味著遇到訪問高峰,或者大量的服務(wù)器失效。一種可行的措施是拒絕新的用戶,并且維持舊的連接。
so_id <integer>
返回 socket 的 id。
用于 frontend,當(dāng)有多個 bind 關(guān)鍵字時,也就是監(jiān)聽了多個地址時,是有用的。
src <ip_address>
返回客戶端的 IPv4 地址。
一般用于對某些資源進(jìn)行訪問限制,比如統(tǒng)計(jì)數(shù)據(jù)資源。
src_port <integer>
返回客戶端的 TCP source port。用處較少。
srv_id <integer>
返回 server 的 id。
可用于 frontend 和 backend
srv_is_up(<server>)
srv_is_up(<backend>/<server>)
當(dāng)指定 backend 或當(dāng)前 backend 中的 server 正在運(yùn)行,返回 true,否則返回 false。
不接受參數(shù)。
當(dāng)健康檢查報告了一個外部狀態(tài)時(eg: a geographical site's availability),采取一定的動作。
7.5.2 四層匹配
第二部分的測試區(qū)域,基于緩存中找到的數(shù)據(jù),它們在分析的過程可能就會發(fā)生變化。
要求有數(shù)據(jù)被緩存,常用于 “TCP 請求內(nèi)容檢查”:
tcp-request content accept [{if | unless} <condition>]
根據(jù)是否滿足測試條件,判斷接收還是拒絕一個連接。
req_len <integer>
如果在 request buffer 中的數(shù)據(jù)長度匹配了指定的范圍,返回 true。
如果 buffer is changing,那么就不會返回 false。在一個會話剛開始的時候,req_len eq 0 是肯定匹配的。
對于 req_len ge 大于0的整數(shù),會等待數(shù)據(jù)進(jìn)入 buffer,如果 haproxy 確定沒有數(shù)據(jù)進(jìn)來時,才會返回 false。
用于 “TCP 請求內(nèi)容檢查”
req_proto_http
如果在 請求buffer 中的數(shù)據(jù)看起來像是 HTTP 或者能被當(dāng)做 HTTP 進(jìn)行正確解析,返回 true。
通過 “TCP 請求內(nèi)容檢查” 的規(guī)則,可用于調(diào)度 HTTP 流量和 HTTPS 流量到不同的端口地址。
req_rdp_cookie <string>
req_rdp_cookie(name) <string>
遠(yuǎn)程桌面協(xié)議相關(guān)
req_rdp_cookie_cnt <integer>
req_rdp_cookie_cnt(name) <integer>
遠(yuǎn)程桌面協(xié)議相關(guān)
req_ssl_ver <decimal>
如果在請求緩存中的數(shù)據(jù),看起來像 SSL 協(xié)議數(shù)據(jù),而且協(xié)議版本在指定的范圍內(nèi),返回 ture。
支持 SSLv2 hello messages 和 SSLv3 messages。
此測試意在進(jìn)行嚴(yán)格限制,盡量避免被輕易地欺騙。
In particular, it waits for as many bytes as announced in the
message header if this header looks valid (bound to the buffer size)
注意,TLSv1 協(xié)議將被稱為 SSL version 3.1。
用于 “TCP 請求內(nèi)容檢查”
wait_end
等待分析周期結(jié)束,返回 true。
一般與內(nèi)容分析聯(lián)合使用,避免過早返回一個錯誤的結(jié)論。
也可用于延遲某些動作,比如拒絕某些指定的IP地址的動作。
因?yàn)樗赐V挂?guī)則檢查,要么立即返回 true,所以建議在一條規(guī)則的最后使用這個 acl。
注意,默認(rèn)的 ACL "WAIT_END" 總是可用的,不需要預(yù)先聲明。
本測試用于 “TCP 請求內(nèi)容檢查”
示例:
# delay every incoming request by 2 seconds
tcp-request inspect-delay 2s
tcp-request content accept if WAIT_END
# don't immediately tell bad guys they are rejected
tcp-request inspect-delay 10s
acl goodguys src 10.0.0.0/24
acl badguys src 10.0.1.0/24
tcp-request content accept if goodguys
tcp-request content reject if badguys WAIT_END
tcp-request content reject
7.5.3 七層匹配
第三部分的測試區(qū)域,是七層測試。要求對 HTTP 請求進(jìn)行完全的解析之后進(jìn)行。
因?yàn)檎埱蠛晚憫?yīng)都被建立了索引,所以雖然相比四層匹配要求更多的 CPU 資源,但也不會太多。
hdr <string>
hdr(header) <string>
hdr* 匹配所有 headers,hdr*(header) 匹配指定的 header,注意括號里面不能有空格。
指定首部時,其名稱不區(qū)分大小寫;
The header matching complies with RFC2616, and treats all values delimited by commas as separate headers.
對于響應(yīng)首部,使用 shdr();
示例,檢查是否設(shè)置了 "connection: close" :
hdr(Connection) -i close
hdr_beg <string>
hdr_beg(header) <string>
任何一個 header 的值是以 string 起始的,返回 true
對于響應(yīng)首部,使用 shdr_beg();
hdr_cnt <integer>
hdr_cnt(header) <integer>
如果指定的 header 出現(xiàn)的次數(shù)在指定范圍內(nèi),或匹配指定的值,返回 true;
一行 header 語句如果包含多個值,將被多次計(jì)數(shù);
用于檢查指定 header 的是否存在,是否被濫用;
通過拒絕含有多個指定 header 的請求,可阻擋 request smuggling attacks;
對于響應(yīng)首部,使用 shdr_cnt();
hdr_dir <string>
hdr_dir(header) <string>
用于文件名或目錄名匹配,當(dāng)某個 header 包含被 / 分隔的 string,返回 true;
可與 Referer 聯(lián)合使用;
對于響應(yīng)首部,使用 shdr_dir();
hdr_dom <string>
hdr_dom(header) <string>
用于域名匹配,可與 Host header 一起使用;當(dāng)某個 header 包含被 . 分隔的 string,返回 true;
對于響應(yīng)首部,使用 shdr_dom();
hdr_end <string>
hdr_end(header) <string>
當(dāng)任何一個 header 的值是以 string 結(jié)束的,返回 true
對于響應(yīng)首部,使用 shdr_end();
hdr_ip <ip_address>
hdr_ip(header) <ip_address>
當(dāng)某個 header 的值包含匹配 <ip_address> 的值,返回 true;
通常與 X-Forwarded-For or X-Client-IP 一起使用;
對于響應(yīng)首部,使用 shdr_ip();
hdr_len <integer>
hdr_len(<header>) <integer>
至少有一個 header 的 length 與指定的值或者范圍匹配時,返回 true;
對于響應(yīng)首部,使用 shdr_len();
hdr_reg <regex>
hdr_reg(header) <regex>
有一個 header 與正則表達(dá)式匹配時,返回 true,可在任何時候使用;
注意,正則匹配比其他匹配更慢;
對于響應(yīng)首部,使用 shdr_reg();
hdr_sub <string>
hdr_sub(header) <string>
有一個 header 包含其中一個 string 時,返回 true;
對于響應(yīng)首部,使用 shdr_sub();
hdr_val <integer>
hdr_val(header) <integer>
有一個 header 起始的數(shù)字,與指定值或范圍匹配;可用于限制 content-length,只接受合理長度的請求;
對于響應(yīng)首部,使用 shdr_val();
http_auth(userlist)
http_auth_group(userlist) <group> [<group>]*
從客戶端收到的認(rèn)證信息與 userlist 中記錄的匹配時,返回 true;
目前只支持 http basic auth;
http_first_req
如果處理的請求是連接的第一個請求,返回 true;
method <string>
用于檢查 HTTP 請求的方法,匹配時,返回 true;
path <string>
請求中的 path 部分(以 / 起始,到 ? 為止的部分),與某個 string 相等時,返回 true;
可用于匹配某個文件,比如:/favicon.ico
path_beg <string>
當(dāng) path 以某個 string 為起始,返回 true;可用于發(fā)送某些目錄名到 alternative backend;
path_dir <string>
當(dāng) path 中包含以 / 分隔的 string 時,返回 true;可用于匹配文件名或目錄名;
path_dom <string>
當(dāng) path 中包含以 . 分隔的 string 時,返回 true;可用于域名匹配;
path_end <string>
當(dāng) path 以某個 string 為結(jié)束時,返回 true;可用于控制文件擴(kuò)展名;
path_len <integer>
當(dāng) path 的長度與指定值或范圍匹配時,返回 true;可用于檢測 abusive requests;
path_reg <regex>
當(dāng) path 與正則表達(dá)式匹配時,返回 true;
path_sub <string>
當(dāng) path 包含某個 string 時,返回 true;可用于檢測 path 中的指定模式,比如 “../”;
req_ver <string>
用于檢查 HTTP 請求的版本,比如 1.0;
status <integer>
用于檢查 HTTP response 的狀態(tài)碼,比如 302;
可根據(jù)狀態(tài)碼做一定的動作,比如,如果 response 的狀態(tài)碼不是 3xx,則刪除 Location header;
url <string>
應(yīng)用于請求中的整個 URL;真正的用處是匹配 *,已有一個預(yù)定義的 ACL: HTTP_URL_STAR;
url_beg <string>
當(dāng) URL 以某個 string 起始時,返回 true;可用于檢查是否以 / 或者某個協(xié)議的 scheme 起始;
url_dir <string>
如果 URL 中包含以 / 分隔的 string,返回 true;用于文件名和目錄名匹配;
url_dom <string>
如果 URL 中包含以 . 分隔的 string,返回 true;用于域名匹配;
url_end <string>
當(dāng) URL 以某個 string 結(jié)束時,返回 true;用處很少;
url_ip <ip_address>
用于檢查 HTTP 請求中,絕對 URI 中所指定的 IP 地址;可用于根據(jù) IP 地址做資源的訪問限制;
跟 http_proxy 一起用時可發(fā)揮作用;
url_len <integer>
當(dāng) URL 的長度與指定值或范圍匹配時,返回 true;可用于檢測 abusive requests;
url_port <integer>
用于檢查 HTTP 請求中,絕對 URI 中所指定的 PORT 地址;可用于根據(jù) PORT 地址做資源的訪問限制;
跟 http_proxy 一起用時可發(fā)揮作用;
注意,如果請求中沒有指定端口,則表示端口為 80;
url_reg <regex>
當(dāng) URL 與正則表達(dá)式匹配時,返回 true;
url_sub <string>
當(dāng) URL 包含某個 string 時,返回 true;可用于檢查 query string 中的某些 pattern;
7.5.4 預(yù)定義的 ACLs
預(yù)定義的 ACLs 不需要聲明,可以直接使用。它們的命名都是大寫字母。
ACL name 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_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_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
7.5.5 使用 ACLs 構(gòu)造 conditions
有些 actions 只在滿足了有效的條件時才能執(zhí)行;條件是 ACLs 的邏輯組合;
有三個可用的邏輯運(yùn)算符:
- AND(隱含的)
- OR(顯式地,使用 or 或者 || 運(yùn)算符)
- Negtion(!)
一個條件的構(gòu)成:
[!]acl1 [!]acl2 ... [!]acln { or [!]acl1 [!]acl2 ... [!]acln } ...
actions 配合條件:
actions if/unless conditons
例如,構(gòu)造一個條件,希望阻擋滿足條件 HTTP 請求,滿足如下條件之一時,拒絕該請求:
不是 OPTIONS 請求,但 URL 為 *;
是 POST 請求,但沒有 content-length 首部;
是 GET or HEAD 請求,但 content-length > 0;
請求 method,是除了 GET/HEAD/POST/OPTIONS 的請求;
規(guī)則是:
acl missing_cl hdr_cnt(Content-length) eq 0
block if HTTP_URL_STAR !METH_OPTIONS || METH_POST missing_cl
block if METH_GET HTTP_CONTENT
block unless METH_GET or METH_POST or METH_OPTIONS
例2.
建立 url_static 測試,當(dāng) path 以 /static、/images、/img、/css 起始,或者以 .gif、.png、.jpg、.css、.js 結(jié)尾,返回 true;
建立 host_www 測試,當(dāng)請求的 Host 首部字段以 www 起始,返回 true,忽略大小寫;
建立 host_static 測試,當(dāng)請求的 Host 首部字段以 img. video. download. ftp. 之一為起始,返回 true,忽略大小寫;
滿足 host_static 測試,或者 host_www AND url_static 測試的請求,轉(zhuǎn)發(fā)給 static backend;
只滿足 host_www 測試的請求,轉(zhuǎn)發(fā) www backend;
acl url_static path_beg /static /images /img /css
acl url_static path_end .gif .png .jpg .css .js
acl host_www hdr_beg(host) -i www
acl host_static hdr_beg(host) -i img. video. download. ftp.
# now use backend "static" for all static-only hosts, and for static urls
# of host "www". Use backend "www" for the rest.
use_backend static if host_static or host_www url_static
use_backend www if host_www
HAProxy 支持使用匿名的 ACLs;不需要事先聲明;它們必須被 { ACLs } 括起來,注意空格;例如:
The following rule :
acl missing_cl hdr_cnt(Content-length) eq 0
block if METH_POST missing_cl
Can also be written that way :
block if METH_POST { hdr_cnt(Content-length) eq 0 }
一般而言,不建議使用匿名的 ACL,因?yàn)楦菀壮霈F(xiàn)錯誤;
只有對于簡單 ACLs,比如匹配一個 src IP地址,這時使用匿名更容易閱讀:
With named ACLs :
acl site_dead nbsrv(dynamic) lt 2
acl site_dead nbsrv(static) lt 2
monitor fail if site_dead
With anonymous ACLs :
monitor fail if { nbsrv(dynamic) lt 2 } || { nbsrv(static) lt 2 }
7.5.6 模式提取
The stickiness features relies on pattern extraction in the request and
response. Sometimes the data needs to be converted first before being stored,
for instance converted from ASCII to IP or upper case to lower case.
All these operations of data extraction and conversion are defined as
"pattern extraction rules". A pattern rule always has the same format. It
begins with a single pattern fetch word, potentially followed by a list of
arguments within parenthesis then an optional list of transformations. As
much as possible, the pattern fetch functions use the same name as their
equivalent used in ACLs.
The list of currently supported pattern fetch functions is the following :
src This is the source IPv4 address of the client of the session.
It is of type IP and only works with such tables.
dst This is the destination IPv4 address of the session on the
client side, which is the address the client connected to.
It can be useful when running in transparent mode. It is of
type IP and only works with such tables.
dst_port This is the destination TCP port of the session on the client
side, which is the port the client connected to. This might be
used when running in transparent mode or when assigning dynamic
ports to some clients for a whole application session. It is of
type integer and only works with such tables.
hdr(name) This extracts the last occurrence of header <name> in an HTTP
request and converts it to an IP address. This IP address is
then used to match the table. A typical use is with the
x-forwarded-for header.
The currently available list of transformations include :
lower Convert a string pattern to lower case. This can only be placed
after a string pattern fetch function or after a conversion
function returning a string type. The result is of type string.
upper Convert a string pattern to upper case. This can only be placed
after a string pattern fetch function or after a conversion
function returning a string type. The result is of type string.
ipmask(mask) Apply a mask to an IPv4 address, and use the result for lookups
and storage. This can be used to make all hosts within a
certain mask to share the same table entries and as such use
the same server. The mask can be passed in dotted form (eg:
255.255.255.0) or in CIDR form (eg: 24).
動靜分離示例
動靜分離示例:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 30000
listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js
use_backend static_servers if url_static
default_backend dynamic_servers
backend static_servers
balance roundrobin
server imgsrv1 172.16.200.7:80 check maxconn 6000
server imgsrv2 172.16.200.8:80 check maxconn 6000
backend dynamic_servers
cookie srv insert nocache
balance roundrobin
server websrv1 172.16.200.7:80 check maxconn 1000 cookie websrv1
server websrv2 172.16.200.8:80 check maxconn 1000 cookie websrv2
server websrv3 172.16.200.9:80 check maxconn 1000 cookie websrv3