天天看點

haproxy ACL

文章目錄

      • haproxy ACL
        • 1. ACL配置選項
          • 1.1:ACL-Name
          • 1.2:ACL-criterion
          • 1.3:ACL-flags
          • 1.4:ACL-operator
          • 1.5:ACL-value
        • 2. ACL調用方式
        • 3. ACL示例-域名比對
        • 4、ACL示例-基于源IP或子網排程通路
        • 5、ACL示例-基于源位址的通路控制
        • 6. ACL示例-比對浏覽器類型
        • 7. ACL示例-基于檔案字尾名實作動靜分離
        • 8. ACL-比對通路路徑實作動靜分離
        • 9. ACL示例-基于ACL的HTTP通路控制
        • 10. ACL示例-預定義ACL使用
          • 10.1:預定義ACL
          • 10.2:預定義ACL使用

haproxy ACL

​ 通路控制清單(ACL,Access Control Lists)是一種基于包過濾的通路控制技術,它可以根據設定的條件對經過伺服器傳輸的資料包進行過濾(條件比對),即對接收到的封包進行比對和過濾,基于請求封包頭部中的源位址、源端口、目标位址、目标端口、請求方法、URL、檔案字尾等資訊内容進行比對并執行進一步操作,允許其通過或丢棄。

文檔

1. ACL配置選項

acl   <aclname> <criterion>   [flags]     [operator]   [<value>]
acl     名稱		比對規範      比對模式     具體操作符    操作對象類型
           
1.1:ACL-Name
acl   image_service hdr_dom(host)   -i   img.magedu.com	
           

ACL名稱,可以使用大字母A-Z、小寫字母a-z、數字0-9、冒号:、點. 、中橫線和下劃線,并且嚴格區分大小寫,必須Image_site和image_site完全是兩個acl。

1.2:ACL-criterion

​ 定義ACL比對規範

完全比對字元串 hdr([<name> [,<occ>]]) 官方7.1節中

 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.magedu.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
           
1.3:ACL-flags

ACL basics: 官方7.1 節中

The following ACL flags are currently supported :

-i 不區分大小寫
 -m 使用指定的pattern比對方法
 -n 不做DNS解析
 -u 禁止acl重名,否則多個同名ACL比對或關系
           
1.4:ACL-operator

整數比較:官方7.12 節 Matching integers

eq、ge、gt、le、lt
           

字元比較:在官方7.13 節中 Matching strings

- 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進行比對
           
1.5:ACL-value

value的類型

ACL basics: 官方7.1 節中

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.magedu.com 
  suffix-字尾比較
  prefix-字首比較
  subdir-路徑, /wp-includes/js/jquery/jquery.js
  domain-域名,www.magedu.com

- regular expression #正規表達式
- hex block #16進制
           

2. ACL調用方式

Using ACLs to form conditions 官文7.2

- 與:隐式(預設)使用(且)
- 或:使用“or” 或 “||”表示
- 否定:使用“!“ 表示
           

3. ACL示例-域名比對

listen web_host
        bind 192.168.1.101:80
        mode http
        balance roundrobin
        log global
        option httplog
        acl web_host hdr_dom(host)   www.mage.net
        use_backend mage_host if web_host
        default_backend default_web

backend mage_host
        mode http
        server web1 192.168.1.103 check inter 2000 fall 3 rise 5

backend default_web
        mode http
        server web1 192.168.1.104:80 check inter 2000 fall 3 rise 5
           

驗證:

​ 在用戶端192.168.1.105 中修改hosts

192.168.1.101 www.mage.net www.xx.com
[email protected]:~# curl  www.mage.net
103 index

[email protected]:~# curl  www.xx.com
104 index


[email protected]:~# curl   192.168.1.101
104 index
           

4、ACL示例-基于源IP或子網排程通路

​ 将指定的源位址排程至指定的web伺服器組

listen web_host
        bind 192.168.1.101:80,172.16.1.21:80
        mode http
        balance roundrobin
        log global
        option httplog
        acl ip_range_test src 172.16.0.0/16 192.168.1.103
        use_backend mage_host if ip_range_test
        default_backend default_web

backend mage_host
        mode http
        server web1 192.168.1.103 check inter 2000 fall 3 rise 5

backend default_web
        mode http
        server web1 192.168.1.104:80 check inter 2000 fall 3 rise 5
           

驗證

[email protected]:~# curl  172.16.1.21
103 index
[email protected]:~# curl  192.168.1.101
104 index
           

5、ACL示例-基于源位址的通路控制

listen web_host
        bind 192.168.1.101:80,172.16.1.21:80
        mode http
        balance roundrobin
        log global
        option httplog
        acl block_test src 192.168.1.103 192.168.0.0/21
        block if block_test
        default_backend default_web

backend mage_host
        mode http
        server web1 192.168.1.103 check inter 2000 fall 3 rise 5

backend default_web
        mode http
        server web1 192.168.1.104:80 check inter 2000 fall 3 rise 5

           

驗證

[email protected]:~# curl 192.168.1.101
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>

[email protected]:~# curl  172.16.1.21
104 index
           

6. ACL示例-比對浏覽器類型

IE11 浏覽器 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
谷歌浏覽器 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
火狐浏覽器User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0

上面3個不同的浏覽器,useragent 值是不一樣的,找出他們獨一無二的部分就可以實作基于不同浏覽器 進行的比對。

比如火狐浏覽器就有 “ Firefox”, IE的那個就有“Trident” 。不可以用數字,因為浏覽器的版本會發生變化的

例子:

當使用者通路192.168.1.101時 ,

​ 如果用戶端是IE就跳轉到百度;

​ 如果用戶端是火狐就跳轉到天貓;

​ 其他的浏覽器,預設通路192.168.1.104

listen web_host
        bind 192.168.1.101:80
        mode http
        balance roundrobin
        log global
        option httplog

        acl redirect_IE hdr(User-Agent) -m sub -i "Trident"
        redirect prefix https://www.baidu.com/ if redirect_IE

        acl redirect_fox hdr(User-Agent) -m sub -i "Firefox"
        redirect prefix https://www.taobao.com/ if redirect_fox

        default_backend default_web


backend default_web
        mode http
        server web1 192.168.1.104:80 check inter 2000 fall 3 rise 5

           

驗證

haproxy ACL
haproxy ACL
haproxy ACL

7. ACL示例-基于檔案字尾名實作動靜分離

listen web_host
	bind 192.168.1.101: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.1.103 check inter 2000 fall 3 rise 5
  
backend image_server_host
	mode http
	server web1 192.168.1.104 check inter 2000 fall 3 rise 5
  
backend default_web
	mode http
	server web1 192.168.1.102:80 check inter 2000 fall 3 rise 5
           

驗證:

[email protected]:~# curl 192.168.1.101/test.php
test
           
haproxy ACL

8. ACL-比對通路路徑實作動靜分離

listen web_host
	bind 192.168.1.101: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.1.104 check inter 2000 fall 3 rise 5

backend default_web
	mode http
	server web1 192.168.1.102:80 check inter 2000 fall 3 rise 5
           

當 通路 http://192.168.1.101/static/1.jpg 效果和上面的一樣

9. ACL示例-基于ACL的HTTP通路控制

listen web_host
	bind 192.168.1.101: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
	
	acl badguy_deny src 192.168.1.102
	http-request deny if badguy_deny
	http-request allow
	default_backend default_web 

backend static_path_host
	mode http
	server web1 192.168.1.104 check inter 2000 fall 3 rise 5

backend default_web
	mode http
	server web1 192.168.1.102:80 check inter 2000 fall 3 rise 5
           

驗證:

[email protected]:~# curl --head http://192.168.1.101/static/1.jpg
HTTP/1.1 403 Forbidden
content-length: 93
cache-control: no-cache
content-type: text/html
connection: close
           

10. ACL示例-預定義ACL使用

官方文檔

10.1:預定義ACL
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_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
10.2:預定義ACL使用

例子:當滿足是靜态檔案且請求協定是http 1.1的才排程到存放靜态資源的伺服器上

listen web_host
	bind 192.168.1.101: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.1.103 check inter 2000 fall 3 rise 5

backend static_path_host
	mode http
	server web1 192.168.1.104 check inter 2000 fall 3 rise 5

backend default_web
	mode http
	server web1 192.168.1.102:80 check inter 2000 fall 3 rise 5
           

注意: acl的調用方式預設是“且“ , 而且還隐藏了"acl true" true 的關鍵字

上例中若是改為use_backend static_path_host if HTTP_1.0 TRUE static_path ,則不可以通路

haproxy ACL

繼續閱讀