1、ACL定义:

访问控制列表,用于实现基于请求报文的首部、响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性。其配置法则通常分为两步,首先去定义ACL ,即定义一个测试条件,而后在条件得到满足时执行某特定的动作,如阻止请求或转发至某特定的后端。
acl:对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作。

•acl <aclname> <criterion> [flags] [operator] [<value>]
•acl 名称条件条件标记位具体操作符操作对象类型
•acl image_service hdr_dom(host) -i img.magedu.com
•ACL名称,可以使用大字母A-Z、小写字母a-z、冒号:、点.、中横线和下划线,并且严格区分大小写,必须Image_site和image_site完全是两个acl。



2、ACL derivatives :

•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>]]):子串匹配



3、<criterion> :匹配条件

(1)dst 目标IP
(2)dst_port 目标PORT
(3)src 源IP
(4)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

演示:

frontend web
        bind 192.168.37.17:80
        mode http
#       acl www_web_page hdr_dom(host)  -i www.magedu.net
#       acl mobile_web_page hdr_dom(host)  -i mobile.magedu.net
        acl ip_range_test src 192.168.37.7 192.168.0.0/24   此源IP地址或者IP段访问时,都跳转至后端服务器的192.168.37.27地址上。
        use_backend web2 if ip_range_test
        default_backend     backup_web_host
 
backend web2
        server web1 192.168.37.27:80 weight 1 check  port 80 inter 3s fall 3 rise 5
        #server web1 192.168.37.37:80 weight 1 check  port 80 inter 3s fall 3 rise 5

HAproxy无法访问 haproxy页面_nginx

在客户端(192.168.37.7)访问haproxy效果,就会一直访问192.168.37.27地址。

HAproxy无法访问 haproxy页面_HAproxy无法访问_02

当我们知道客户端的IP地址时,也可以进行拒绝客户端的访问,加上block选项。

HAproxy无法访问 haproxy页面_php_03

查看访问效果,此时客户端被拒绝访问。

HAproxy无法访问 haproxy页面_php_04



4、<flags>-条件标记

-i 不区分大小写
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名ACL匹配或关系



5、[operator]-操作符

整数比较:eq、ge、gt、le、lt

字符比较:

-exact match (-m str) :字符串必须完全匹配模式
-substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配
-prefix match (-m beg) :在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配
-suffix match (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配
-subdirmatch (-m dir) :查看提取出来的用斜线分隔(“/”)的字符串,如果其中任何一个匹配,则ACL进行匹配
-domain match (-m dom) :查找提取的用点(“.”)分隔字符串,如果其中任何一个匹配,则ACL进行匹配



6、<value>的类型

-Boolean #布尔值false,true
-integer or integer range #整数或整数范围,比如用于匹配端口范围,1024~32768
-IP address / network #IP地址或IP范围, 192.168.0.1 ,192.168.0.1/24

-string用法:

exact    –精确比较
substring  —子串    
suffix   -后缀比较
prefix   -前缀比较
subdir  -路径,    /wp-includes/js/jquery/jquery.js
domain  -域名,www.magedu.com
-regular expression     #正则表达式
-hex block     #16进制



常用示例



ACL基于域名匹配

frontend web_80
	 mode http
	 bind 172.16.0.8:80
	 log global
	#acl
	 acl pc.domain hdr_dom(host) www.ssy.org
	 acl m.domain hdr_dom(host) m.ssy.org 
	 use_backend pc_host if pc.domain
	 use_backend m_host if m.domain
	 default_backend pc_host   #默认调度

backend pc_host
  	server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  	server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

cat /etc/hosts
172.16.0.8 m.ssy.org www.ssy.org
curl www.ssy.org
10.0.0.7 Apache
curl m.ssy.org
10.0.0.17 Nginx



ACL基于源IP地址或子网匹配

frontend web_80
	 mode http
	 bind 172.16.0.8:80
	 log global
	 acl pc.domain hdr_dom(host) www.ssy.org
	 acl m.domain hdr_dom(host) m.ssy.org 
	 acl ip_host src 172.16.0.0/16 10.0.0.6  #匹配源IP来自172.16.0.0/24网段
	 use_backend pc_host if ip_host  #调度到后端pc_host,此优先级比下面的高
	 use_backend pc_host if pc.domain
	 use_backend m_host if m.domain
	 default_backend pc_host

backend pc_host
  	server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  	server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl 172.16.0.8  #连续三次curl都是后端10.0.0.7
10.0.0.7 Apache



ACl基于浏览器类型匹配

frontend web_80
	 mode http
	 bind 172.16.0.8:80
	 log global
	 acl use_agent hdr_sub(User-agent) -i curl wget
	 acl use_agent_ab hdr_sub(User-agent) -i Apachebench  
	 http-request deny if use_agent_ab           #使用ab命令就拒绝 
	 redirect prefix www.baidu.com if use_agent  #使用curl或wget就302重定向
	 default_backend pc_host     

backend pc_host
  	server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  	server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl  -I  172.16.0.8
   HTTP/1.1 302 Found
   content-length: 0
   location: www.baidu.com/ 
ab  -c1  -n1  http://172.16.0.8/ 
   Non-2xx responses:      1
curl -A apachebench 172.16.0.8
   <html><body><h1>403 Forbidden</h1>



ACL-基于后缀名实现动静分离

frontend web_80
	 mode http
	 bind 172.16.0.8:80
	 log global
	 acl static path_end -i .html .css .jpg .png
	 acl php path_end -i .php
	 use_backend pc_host if php   
	 use_backend m_host if static
	 default_backend pc_host
	#也可以使用此方式
	# acl static path_beg  -i /static /images /javascript
	# use_backend m_host if static
	# default_backend pc_host

backend pc_host
  	server 10.0.0.7  10.0.0.7:80  check inter 3s fall 2 rise 3 cookie web1
backend m_host
  	server 10.0.0.17 10.0.0.17:80 check inter 3s fall 2 rise 3 cookie web2

curl  172.16.0.8/yyy.html
   static
curl  172.16.0.8/yyy.php
   php



实战演示

frontend web
        bind 192.168.37.17:80
        mode http
        acl www_web_page hdr_dom(host)  -i www.magedu.net     访问www.magedu.net时,跳转至192.168.37.27后端服务器
        acl mobile_web_page hdr_dom(host)  -i mobile.magedu.net  访问mobile.magedu.net时,跳转至192.168.37.37后端服务器
 
        use_backend pc_web_host if www_web_page
        use_backend mobile_web_host if mobile_web_page
        default_backend     backup_web_host   访问其他域名时,默认跳转至192.168.37.47后端服务器。
 
backend pc_web_host
        server web1 192.168.37.27:80 weight 1 check  port 80 inter 3s fall 3 rise 5
        #server web1 192.168.37.37:80 weight 1 check  port 80 inter 3s fall 3 rise 5
 
backend mobile_web_host
        #server web1 192.168.37.27:80 weight 1 check  port 80 inter 3s fall 3 rise 5
        server web1 192.168.37.37:80 weight 1 check  port 80 inter 3s fall 3 rise 5
 
backend backup_web_host
        server web1 192.168.37.47:80 weight 1 check  port 80 inter 3s fall 3 rise 5

网页访问效果:

HAproxy无法访问 haproxy页面_nginx_05



7、Acl定义与调用

多个acl作为条件时的逻辑关系:

-与:隐式(默认)使用
-或:使用“or” 或“||”表示
-否定:使用“!“ 表示

示例:

if valid_src valid_port    #与关系
if invalid_src || invalid_port  #或
if ! invalid_src    #非



自定义错误页面

支持200, 400, 403, 408, 500, 502, 503, 504.

errorfile 500 /usr/local/haproxy/html/500.html     #自定义错误页面跳转
errorfile 502 /usr/local/haproxy/html/502.html
errorfile 503 /usr/local/haproxy/html/503.html

自定义错误跳转,直接跳转到指定的服务器IP地址上,此IP地址需要是公网IP,并让其客户端能够访问,否则无法跳转。

errorloc 503 http://192.168.37.27/error_page/503.html



实战演一:

直接在HAProxy机器上创建自定义的错误页面。

errorfile 500 /usr/local/haproxy/html/500.html
errorfile 502 /usr/local/haproxy/html/502.html
errorfile 503 /usr/local/haproxy/html/503.html
frontend web
        bind 192.168.37.17:80
        mode http
        acl www_web_page hdr_dom(host)  -i www.magedu.net
        use_backend mobile_web_host if www_web_page
        default_backend     backup_web_host
 
backend mobile_web_host
        #server web1 192.168.37.27:80 weight 1 check  port 80 inter 3s fall 3 rise 5
        server web1 192.168.37.37:80 weight 1 check  port 80 inter 3s fall 3 rise 5
 
backend backup_web_host
        server web1 192.168.37.47:80 weight 1 check  port 80 inter 3s fall 3 rise 5

HAproxy无法访问 haproxy页面_nginx_06

然后创建错误页面目录及文件

[root@centos_17html]#mkdir /usr/local/haproxy/html
[root@centos_17html]#cd /usr/local/haproxy/html/
[root@centos_17html]#echo error 503 >  503.html
[root@centos_17html]#echo error 502 >  502.html
[root@centos_17html]#echo error 500 >  500.html

访问网页:

HAproxy无法访问 haproxy页面_HAproxy无法访问_07



实战演示二:

自定义错误跳转,直接跳转到指定的服务器URL上,此IP地址需要是公网域名,并让其客户端能够访问,否则无法跳转。

errorloc 503 http://192.168.37.37/error_page/503.html   指定跳转至192.168.37.37的服务器上。

HAproxy无法访问 haproxy页面_HAproxy无法访问_08

在后端要跳转的服务器上创建一个错误提示:

[root@centos37monitor_page]#mkdir /var/www/html/error_page
[root@centos37monitor_page]#echo Custerm error 503 > /var/www/html/error_page/503.html

停止掉要访问的后端服务器:systemctl stop httpd

验证访问效果:

HAproxy无法访问 haproxy页面_HAproxy无法访问_09