haproxy 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

root@z5:~# curl  www.mage.net
103 index

root@z5:~# curl  www.xx.com
104 index


root@z5:~# 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

验证

root@z5:~# curl  172.16.1.21
103 index
root@z5:~# 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

验证

root@z6:~# curl 192.168.1.101
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>

root@z6:~# 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

验证

mark

mark

mark

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

验证:

root@z5:~# curl 192.168.1.101/test.php
test

mark

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

验证:

root@z2:~# 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 nameEquivalent toUsage
FALSEalways_falsenever match
HTTPreq_proto_httpmatch if protocol is valid HTTP
HTTP_1.0req_ver 1.0match HTTP version 1.0
HTTP_1.1req_ver 1.1match HTTP version 1.1
HTTP_CONTENThdr_val(content-length) gt 0match an existing content-length
HTTP_URL_ABSurl_reg [/:]*😕/match absolute URL with scheme
HTTP_URL_SLASHurl_beg /match URL beginning with “/”
HTTP_URL_STARurl *match URL equal to “*”
LOCALHOSTsrc 127.0.0.1/8match connection from local host
METH_CONNECTmethod CONNECTmatch HTTP CONNECT method
METH_DELETEmethod DELETEmatch HTTP DELETE method
METH_GETmethod GET HEADmatch HTTP GET or HEAD method
METH_HEADmethod HEADmatch HTTP HEAD method
METH_OPTIONSmethod OPTIONSmatch HTTP OPTIONS method
METH_POSTmethod POSTmatch HTTP POST method
METH_PUTmethod PUTmatch HTTP PUT method
METH_TRACEmethod TRACEmatch HTTP TRACE method
RDP_COOKIEreq_rdp_cookie_cnt gt 0match presence of an RDP cookie
REQ_CONTENTreq_len gt 0match data in the request buffer
TRUEalways_truealways match
WAIT_ENDwait_endwait 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 ,则不可以访问

mark

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值