马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
HAProxy 高级功能
介绍 HAProxy 高级设置及实用案例
ACL
访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条 件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头 部中的源地点、源端口、目标地点、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行 进一步操作,好比允许其通过或丢弃。
官方资助:
http://cbonte.github.io/haproxy-dconv/2.1/configuration.html#7
http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7
定义ACL设置选项
acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 详细操作符 操作对象类型
ACL-Name
- acl image_service hdr_dom(host) -i img.wang.com
- #ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如:my_acl和My_Acl就是两个完全不同的acl
复制代码
ACL-criterion
定义ACL匹配规范,即:判断条件
- hdr string,提取在一个HTTP请求报文的首部
- hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出
- 现次数
- hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
- hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
- hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name
- hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
- hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
- hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
- hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配
- #示例:
- hdr(<string>) 用于测试请求头部首部指定内容
- hdr_dom(host) 请求的host名称,如 www.wang.com,m.wang.com
- hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.
- hdr_end(host) 请求的host结尾,如 .com .net .cn
- #示例:
- acl bad_agent hdr_sub(User-Agent) -i curl wget
- http-request deny if bad_agent
- #block if bad_agent 2.1版本后不再支持,用上面替代
- hdr(host) ==>www.wang.org:8080
- hdr_domain(host) ==> www.wang.org
- #有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www
- acl short_form hdr_beg(host) www.
- acl alternate1 hdr_beg(host) -m beg www.
- acl alternate2 hdr_dom(host) -m beg www.
- acl alternate3 hdr(host) -m beg www.
- base : string
- #返回第一个主机头和请求的路径部分的连接,该请求从主机名开始,并在问号之前结束,对虚拟主机有用,下面的例子中是两个#中间的内容,实际#是没有的
- <scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
- 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 : string
- #提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)
- <scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
- path : exact string match
- path_beg : prefix match #请求的URL开头,如/static、/images、/img、/css
- path_end : suffix match #请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg
- path_dom : domain match
- path_dir : subdir match
- path_len : length match
- path_reg : regex match
- path_sub : substring match
- #示例:
- path_beg -i /haproxy-status/
- path_end .jpg .jpeg .png .gif
- path_reg ^/images.*\.jpeg$
- path_sub image
- path_dir jpegs
- path_dom www.wang.org
-
- url : string
- #提取请求中的整个URL。一个典型的应用是具有预取能力的缓存,以及需要从数据库聚合多个信息并将它们保存在缓存中的网页门户入口,推荐使用path
- 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
-
- src #源IP
- src_port #源PORT
- dst #目标IP
- dst_port #目标PORT
- #示例:
- acl invalid_src src 10.0.0.7 192.168.1.0/24
- acl invalid_src src 172.16.0.0/24
- http-request deny if ! invalid_src
- acl invalid_port src_port 0:1023
- status : integer #返回在响应报文中的状态码
- #七层协议
- acl valid_method method GET HEAD
- http-request deny if ! valid_method
复制代码
ACL-flags
ACL匹配模式
- -i 不区分大小写
- -m 使用指定的pattern匹配方法,beg 开头,end 结尾
- -n 不做DNS解析
- -u 禁止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--> www.wang.com
- exact #精确比较
- substring #子串
- suffix #后缀比较
- prefix #前缀比较
- subdir #路径, /wp-includes/js/jquery/jquery.js
- domain #域名,www.wang.com
- - regular expression #正则表达式
- - hex block #16进制
复制代码 多个ACL的组合调用方式
多个ACL的逻辑处置处罚
与:隐式(默认)使用
或:使用“or" 或 “||"表示
否定:使用 "!" 表示
多个ACL调用方式:
#示例:
if valid_src valid_port #与关系,ACL中A和B都要满足为true,默认为与
if invalid_src || invalid_port #或,ACL中A大概B满足一个为true
if ! invalid_src #非,取反,不满足ACL才为true
ACL示例:域名匹配
- [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl pc_domain hdr_dom(host) -i www.wang.org
- acl mobile_domain hdr_dom(host) -i mobile.wang.org
- ###################### acl hosts #################################
- use_backend pc_hosts if pc_domain
- use_backend mobile_hosts if mobile_domain
- default_backend pc_hosts #所有ACL都不匹配,则使用的默认backend
-
- ###################### backend hosts #############################
- backend mobile_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend pc_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码 测试结果:
- [root@centos6 ~]#cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 centos6.localdomain
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 10.0.0.7 mobile.wang.org www.wang.org wang.org
- [root@centos6 ~]#curl www.wang.org
- 10.0.0.27
- [root@centos6 ~]#curl mobile.wang.org
- 10.0.0.17
- [root@centos6 ~]#curl wang.org
- 10.0.0.27
复制代码 ACL示例:基于源IP或子网调理访问
将指定的源地点调理至指定的web服务器组。
- root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl pc_domain hdr_dom(host) -i www.wang.org
- acl mobile_domain hdr_dom(host) -i mobile.wang.org
- acl ip_range_test src 172.18.0.0/16 10.0.0.6 #基于源地址的ACL,定义多个ACL的顺序无关
- acl ip_range_test2 src 172.18.0.200
- ###################### acl hosts #################################
- use_backend pc_hosts if ip_range_test #放在前面的ACL规则优先生效,引用ACL时,严格的ACL应放在前面
- use_backend pc_hosts if pc_domain
- use_backend mobile_hosts if mobile_domain
- default_backend pc_hosts
-
- ###################### backend hosts #############################
- backend mobile_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend pc_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码
测试结果
- [root@centos6 ~]#hostname -I
- 10.0.0.6
- [root@centos6 ~]#curl www.wang.org
- 10.0.0.27
- [root@internet ~]#curl -H "HOST: www.wang.org" 10.0.0.7
- 10.0.0.27
- [root@centos6 ~]#curl mobile.wang.org
- 10.0.0.27
- [root@centos6 ~]#curl wang.org
- 10.0.0.27
- [root@centos8 ~]#curl mobile.wang.org
- 10.0.0.17
- [root@centos8 ~]#curl www.wang.org
- 10.0.0.27
- [root@centos8 ~]#curl wang.org
- 10.0.0.27
复制代码
ACL示例:基于源地点的访问控制
- listen web_host
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl acl_deny_src src 10.0.0.6 192.168.0.0/24
- #acl lan src 10.0.0.0/24
- ###################### acl hosts #################################
- http-request deny if acl_deny_src
- #http-request deny if ! lan #取反
- #block if acl_deny_src #2.1版本后,不再支持block
-
- #http-request allow
- default_backend default_web
- ###################### backend hosts #############################
- backend wang_host
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend default_web
- mode http
- server web1 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码 测试:
- [root@centos6 ~]#curl www.wang.org
- <html><body><h1>403 Forbidden</h1>
- Request forbidden by administrative rules.
- </body></html>
复制代码 ACL示例:匹配浏览器类型
匹配客户端浏览器,将差异类型的浏览器调动至差异的服务器组
范例: 163 网易拒绝curl和wget的访问
- [root@ubuntu2004 ~]#curl -I www.163.com
- HTTP/1.1 403 Forbidden
- Date: Fri, 01 Jan 2021 06:57:22 GMT
- Content-Type: text/html
- Content-Length: 237
- Connection: keep-alive
- Server: web cache
- Expires: Fri, 01 Jan 2021 06:57:22 GMT
- X-Ser: BC17_lt-shandong-zaozhuang-6-cache-1
- Cache-Control: no-cache,no-store,private
- cdn-user-ip: 111.199.184.218
- cdn-ip: 124.132.138.17
- X-Cache-Remote: HIT
- cdn-source: baishan
- [root@ubuntu2004 ~]#curl -I -A "wget" www.163.com
- HTTP/1.1 403 Forbidden
- Date: Fri, 01 Jan 2021 06:58:13 GMT
- Content-Type: text/html
- Content-Length: 237
- Connection: keep-alive
- Server: web cache
- Expires: Fri, 01 Jan 2021 06:58:13 GMT
- X-Ser: BC17_lt-shandong-zaozhuang-6-cache-1
- Cache-Control: no-cache,no-store,private
- cdn-user-ip: 111.199.184.218
- cdn-ip: 124.132.138.17
- X-Cache-Remote: HIT
- cdn-source: baishan
- #不拒绝其它浏览器的访问
- [root@ubuntu2004 ~]#curl -I -A "ApacheBench" www.163.com
- HTTP/1.1 301 Moved Permanently
- Date: Fri, 01 Jan 2021 06:58:41 GMT
- Content-Length: 0
- Connection: keep-alive
- Server: web cache
- Location: https://www.163.com/
- Cache-Control: no-cache,no-store,private
- cdn-user-ip: 111.199.184.218
- cdn-ip: 124.132.138.11
- X-Cache-Remote: MISS
- cdn-source: baishan
复制代码 范例:
- [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl acl_user_agent hdr_sub(User-Agent) -i curl wget #基于浏览器的ACL
- acl acl_user_agent_ab hdr_sub(User-Agent) -i ApacheBench
- ###################### acl hosts #################################
- redirect prefix http://www.baidu.com if acl_user_agent #302 临时重定向至新URL
- http-request deny if acl_user_agent_ab #拒绝ab
- default_backend pc_hosts
- ###################### backend hosts #############################
- backend mobile_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend pc_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
复制代码 范例:
- [root@centos6 ~]#curl -I 10.0.0.7
- HTTP/1.1 302 Found
- content-length: 0
- location: http://10.0.0.8/
- cache-control: no-cache
- [root@centos6 ~]#curl -L 10.0.0.7
- 10.0.0.8
- [root@centos6 ~]#wget -O - -q http://10.0.0.7
- 10.0.0.8
- [root@centos6 ~]#curl -A chrome http://10.0.0.7
- 10.0.0.27
-
- #模拟ab
- [root@centos6 ~]#curl -A ApacheBench 10.0.0.7
- <html><body><h1>403 Forbidden</h1>
- Request forbidden by administrative rules.
- </body></html>
- [root@centos6 ~]#ab -n1 -c 1 http://10.0.0.7/
- This is ApacheBench, Version 2.3 <$Revision: 655654 $>
- Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
- Licensed to The Apache Software Foundation, http://www.apache.org/
- Benchmarking 10.0.0.7 (be patient).....done
复制代码 ACL示例:基于文件后缀名实现动静分离
- [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html #基于文件后缀名的ACL
- acl acl_php path_end -i .php
- ###################### acl hosts #################################
- use_backend mobile_hosts if acl_static
- use_backend app_hosts if acl_php
- default_backend pc_hosts
- ###################### backend hosts #############################
- backend mobile_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend pc_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
- backend app_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
- #分别在后端两台主机准备相关文件
- [root@centos17 ~]#ls /var/www/html
- index.html wang.jpg
- [root@centos27 ~]#cat /var/www/html/test.php
- <?php
- echo "<h1>http://10.0.0.27/test.php</h1>\n";
- ?>
复制代码 ACL示例:匹配访问路径实现动静分离
- [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl acl_static path_beg -i /static /images /javascript #基于路径的ACL
- acl acl_static path_end -i .jpg .jpeg .png .gif .css .js .html .htm #ACL同名为或关系
- acl acl_app path_beg -i /api
- ###################### acl hosts #################################
- use_backend static_hosts if acl_static
- use_backend app_hosts if acl_app
- default_backend app_hosts
- ###################### backend hosts #############################
- backend static_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend app_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
- #创建相关文件
- [root@centos17 ~]#mkdir /var/www/html/static
- [root@centos17 ~]#echo 10.0.0.17 > /var/www/html/static/test.html
- #测试访问
- [root@centos6 ~]#curl 10.0.0.7/static/test.html
- 10.0.0.17
复制代码 ACL示例:预定义ACL使用
官方资助文档: HAProxy version 2.1.12 - Configuration Manual
预定义ACL使用
范例: 禁止TRACE方法和HTTP/1.1协议
- [root@centos6 ~]#curl -I -XTRACE 10.0.0.7/static/test.html
- HTTP/1.1 200 OK
- date: Sat, 04 Apr 2020 02:04:01 GMT
- server: Apache/2.4.6 (CentOS) PHP/5.4.16
- transfer-encoding: chunked
- content-type: message/http
- [root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg
- frontend wang_http_port
- bind 10.0.0.7:80
- mode http
- balance roundrobin
- log global
- option httplog
- ###################### acl setting ###############################
- acl acl_static_path path_beg -i /static /images /javascript
- ###################### acl hosts #################################
- use_backend static_path_hosts if acl_static_path
- http-request deny if METH_TRACE HTTP_1.1 #引用预定义的ACL,多个ACL默认为与关系
- default_backend pc_hosts
- ################### backend hosts ################################
- backend static_path_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend mobile_hosts
- mode http
- server web1 10.0.0.17 check inter 2000 fall 3 rise 5
- backend pc_hosts
- mode http
- server web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
- [root@centos6 ~]#curl -I -XTRACE 10.0.0.7/static/test.html
- HTTP/1.1 403 Forbidden
- content-length: 93
- cache-control: no-cache
- content-type: text/html
- connection: close
- [root@centos6 ~]#curl -I -0 -XTRACE 10.0.0.7/static/test.html
- HTTP/1.1 200 OK
- date: Sat, 04 Apr 2020 02:10:13 GMT
- server: Apache/2.4.6 (CentOS) PHP/5.4.16
- content-type: message/http
- connection: close
- #查看日志
,观察协议版本 - [root@centos17 ~]#tail /var/log/httpd/access_log
- 10.0.0.7 - - [04/Apr/2020:10:11:41 +0800] "TRACE /static/test.html HTTP/1.0" 200
- 230 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.27.1
- zlib/1.2.3 libidn/1.18 libssh2/1.4.2"
- [root@centos6 ~]#curl -i 10.0.0.7/static/test.html
- HTTP/1.1 200 OK
- date: Sat, 04 Apr 2020 02:07:58 GMT
- server: Apache/2.4.6 (CentOS) PHP/5.4.16
- last-modified: Sat, 04 Apr 2020 01:27:45 GMT
- etag: "a-5a26cf0ed4913"
- accept-ranges: bytes
- content-length: 10
- content-type: text/html; charset=UTF-8
- 10.0.0.17
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|