1.什么是Haproxy?
HAProxy是法国开发者 威利塔罗(Willy Tarreau) 在2000年使用C语言开发的一个开源软件,是一款具 备高并发(一万以上)、高性能的TCP和HTTP负载均衡器,支持基于cookie的持久性,自动故障切换,支 持正则表达式及web状态统计
2.Haproxy能做什么?
TCP 和 HTTP反向代理 SSL/TSL服务器 可以针对HTTP请求添加cookie,进行路由后端服务器 可平衡负载至后端服务器,并支持持久连接 支持所有主服务器故障切换至备用服务器 支持专用端口实现监控服务 支持停止接受新连接请求,而不影响现有连接 可以在双向添加,修改或删除HTTP报文首部 响应报文压缩 支持基于pattern实现连接请求的访问控制 通过特定的URI为授权用户提供详细的状态信息 支持http反向代理 支持动态程序的反向代理 支持基于数据库的反向代理
3.不具备哪些功能
正向代理--squid,nginx 缓存代理--varnish web服务--nginx、tengine、apache、php、tomcat UDP--目前不支持UDP协议 单机性能--相比LVS性能较差
4.Haproxy的安装(其中的方法)
4.1环境准备、
[root@node1 ~]#yum -y install gcc openssl-devel pcre-devel systemd-devel wget [root@node1 ~]# wget http://www.lua.org/ftp/lua-5.3.5.tar.gz [root@node1 ~]# tar xf lua-5.3.5.tar.gz -C /usr/local/src [root@node1 ~]# cd /usr/local/src/lua-5.3.5 [root@node1 lua-5.3.5]# make linux test
4.2安装Haproxy
[root@node1 ~]# tar xf haproxy-2.2.9.tar.gz -C /usr/local/src/ [root@node1 ~]# cd /usr/local/src/haproxy-2.2.9/ [root@centos7 haproxy-2.2.9]# make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ [root@centos7 haproxy-2.2.9]# make install PREFIX=/apps/haproxy [root@centos7 haproxy-2.2.9]# ln -s /apps/haproxy/sbin/haproxy /usr/sbin/ 注:make的路径需要与lua的路径相同
5.配置Hparoxy的系统脚本(配置后可以使用systemctl start haproxy启动)
[root@node1 ~]# vim /usr/lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
创建Haproxy配置文件(没有会启动不成功)
#创建自定义的配置文件 [root@node1 ~]# mkdir /etc/haproxy [root@node1 ~]# vim /etc/haproxy/haproxy.cfg #global 全局配置文件 global maxconn 100000 chroot /apps/haproxy stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin #uid 99 #gid 99 user haproxy group haproxy daemon #nbproc 4 #cpu-map 1 0 #cpu-map 2 1 #cpu-map 3 2 #cpu-map 4 3 pidfile /var/lib/haproxy/haproxy.pid log 127.0.0.1 local2 info #默认配置文件 defaults option http-keep-alive option forwardfor maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms #统计变量 listen stats mode http bind 0.0.0.0:9999 stats enable log global stats uri /haproxy-status stats auth haadmin:123456
启动Hparoxy
[root@node1 ~]# mkdir /var/lib/haproxy [root@node1 ~]# useradd -r -s /sbin/nologin -d /var/lib/haproxy haproxy [root@node1 ~]# systemctl enable --now haproxy
6.Hparoxy的使用
6.1Hparoxy配置多进程和socket文件
[root@node1 ~]# vim /etc/haproxy/haproxy.cfgglobalmaxconn 100000chroot /apps/haproxystats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2uid 99gid 99daemonnbproc 2
6.2配置日志文件
#在global配置项定义: log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、 info、debug)listen web_portbind 127.0.0.1:80mode httplog global #开启当前web_port的日志功能,默认不记录日志server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5 # systemctl restart haproxy
6.3Rsyslog**配置**
vim /etc/rsyslog.conf $ModLoad imudp $UDPServerRun 514 ...... # Save haproxy messages also to haproxy.log local2.* /var/log/haproxy.log #!!!! ...... # systemctl restart rsyslog
6.4配置Haproxy的四层循环,一个Haproxy+2个后台服务器
服务器 | IP |
---|---|
Haproxy | 192.168.206.128 |
web1 | 192.168.206.134 |
web2 | 192.168.206.135 |
Haproxy的配置 (配置后端内容+Hparoxy配置文件)
#前端服务 frontend WEB_PORT_80 bind 192.168.150.11:80 #监听端口 mode http #使用协议 use_backend web_prot_http_nodes #后端名称 #后端服务器 backend web_prot_http_nodes #后端+后端名称 mode http #使用协议 option forwardfor server 192.168.150.14 192.168.150.14:80 check inter 3000 fall 3 rise 5 #后端服务器名称 server 192.168.150.15 192.168.150.15:80 check inter 3000 fall 3 rise 5
7.Proxies配置-listen替代frontend+backend**
使用listen替换上面的frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用
#官网业务访问入口 listen WEB_PORT_80bind 192.168.150.11:80mode httpoption forwardforserver web1 192.168.150.14:80 check inter 3000 fall 3 rise 5server web2 192.168.150.15:80 check inter 3000 fall 3 rise 5
8.使用子配置文件保存配置
当业务众多时,将所有配置都放在一个配置文件中,会造成维护困难。可以考虑按业务分类,将配置信息拆分,放在不同的子配置文件中,从而达到方便维护的目的。
#创建子配置目录 [root@node1 ~]# mkdir /etc/haproxy/conf.d/ #创建子配置文件,注意:必须为cfg后缀 [root@node1 ~]# vim /etc/haproxy/conf.d/test.cfg listen WEB_PORT_80bind 192.168.150.11:80mode httpbalance roundrobinserver web1 192.168.150.14:80 check inter 3000 fall 3 rise 5server web2 192.168.150.15:80 check inter 3000 fall 3 rise 5 #添加子配置目录到unit文件中 [root@node1 ~]# vim /lib/systemd/system/haproxy.service [Unit] Description=HAProxy Load Balancer After=syslog.target network.target [Service] ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -c -q ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/ -p /var/lib/haproxy/haproxy.pid ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target [root@centos7 ~]#systemctl daemon-reload [root@centos7 ~]#systemctl restart haproxy
9.Haproxy调度算法总结
HAProxy通过固定参数 balance 指明对后端服务器的调度算法,该参数可以配置在listen或backend选项中。
HAProxy的调度算法分为静态和动态调度算法,但是有些算法可以根据参数在静态和动态算法中相互转换。
静态算法
静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、链接数和响应速度等,且无法实时修改权重,只能靠重启HAProxy生效。可以利用 socat工具对服务器动态权重和其它状态的调整,Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,Socat 的主要特点就是在两个数据流之间建立通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等
范例:利用工具socat 对服务器动态权重调整
[root@node1 ~]# yum -y install socat #配置配置文件 [root@node1 ~]# vim /etc/haproxy/conf.d/test.cfg listen WEB_PORT_80bind 192.168.150.11:80mode httpbalance roundrobinserver web1 192.168.150.14:80 check inter 3000 fall 3 rise 5server web2 192.168.150.15:80 check inter 3000 fall 3 rise 5 weight 3 #查看Haproxy配置的server服务 [root@node1 ~]# echo "show servers state" | socat stdio /var/lib/haproxy/haproxy.sock1 1 # be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord 3 WEB_PORT_80 1 web1 192.168.150.14 2 0 1 1 3 6 3 7 6 0 0 0 - 80 - 3 WEB_PORT_80 2 web2 192.168.150.15 2 0 3 3 3 6 3 7 6 0 0 0 - 80 - #查看权限openlab-test-80为服务名 [root@centos7 ~]# echo "get weight openlab-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock 3 (initial 3) #修改weight,注意只针对单进程有效 [root@centos7 ~]# echo "set weight openlab-test-80/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock [root@centos7 ~]# echo "get weight openlab-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock 2 (initial 3) #将后端服务器禁用,注意只针对单进程有效 [root@centos7 ~]# echo "disable server openlab-test-80/web2" | socat stdio /var/lib/haproxy/haproxy.sock #将后端服务器软下线,即weight设为0 [root@centos7 ~]# echo "set weight openlab-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock #将后端服务器禁用,针对多进程 [root@centos7 ~]# vim /etc/haproxy/haproxy.cfg ...... stats socket /var/lib/haproxy/haproxy1.sock mode 600 level admin process 1 stats socket /var/lib/haproxy/haproxy2.sock mode 600 level admin process 2 nbproc 2 ..... [root@centos7 ~]# echo "disable server openlab-test-80/web2" | socat stdio /var/lib/haproxy/haproxy1.sock [root@centos7 ~]# echo "disable server openlab-test-80/web2" | socat stdio /var/lib/haproxy/haproxy2.sock [root@haproxy ~]# for i in {1..2};do echo "set weight openlab-test-80/webi 10" | socat stdio /var/lib/haproxy/haproxyi.sock;done #如果静态算法,如:static-rr,可以更改weight为0或1,但不支持动态更改weight为其它值,否则会提示下面信息 [root@centos7 ~]# echo "set weight openlab-test-80/web1 0" | socat stdio /var/lib/haproxy/haproxy.sock [root@centos7 ~]# echo "set weight openlab-test-80/web1 1" | socat stdio /var/lib/haproxy/haproxy.sock [root@centos7 ~]# echo "set weight openlab-test-80/web1 2" | socat stdio /var/lib/haproxy/haproxy.sock Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.
算法总结:
static-rr--------->tcp/http 静态 first------------->tcp/http 静态 roundrobin-------->tcp/http 动态 leastconn--------->tcp/http 动态 random------------>tcp/http 动态 以下静态和动态取决于hash_type是否consistent source------------>tcp/http Uri--------------->http url_param--------->http hdr--------------->http rdp-cookie-------->tcp
static-rr
static-rr:基于权重的轮询调度,不支持权重的运行时利用socat进行动态调整及后端服务器慢启动,其后端主机数量没有限制,相当于LVS中的 wrr
案列: listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance static-rrserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
first
first:根据服务器在列表中的位置,自上而下进行调度,但是其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务,因此会忽略服务器的权重设置,此方式使用较少、
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance firstserver web1 10.0.0.17:80 maxconn 2 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
动态算法
动态算法:基于后端服务器状态进行调度适当调整,优先调度至当前负载较低的服务器,且权重可以在haproxy运行时动态调整无需重启。
roundrobin
roundrobin:基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),其每个后端backend中最多支持4095个real server,支持对real server权重动态调整roundrobin为默认调度算法。
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance roundrobinserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
leastconn
leastconn加权的最少连接的动态,支持权重的运行时调整和慢启动,即当前后端服务器连接最少的优先调度(新客户端连接),比较适合长连接的场景使用,比如:MySQL等场景。
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance leastconnserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
random
在1.9版本开始增加一个叫做random的负载平衡算法,其基于随机数作为一致性hash的key,随机负载平衡对于大型服务器场或经常添加或删除服务器非常有用,支持weight的动态调整,weight较大的主机有更大概率获取新请求
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode httplog globalbalance randomserver web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 5
source
源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景
源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash
map-base**取模法**
map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度。缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变,hash-type 指定的默认值为此算法。
取模法配置案例:
listen web_hostbind 10.0.0.7:80,:8801-8810,10.0.0.7:9001-9010mode tcplog globalbalance source #1.先配置sourcehash-type map-based #2.对source 进行map-based模式server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 3server web2 10.0.0.27:80 weight 1 check inter 3000 fall 2 rise 3 [root@haproxy ~]# echo "set weight web_host/10.0.0.27 10" | socat stdio /var/lib/haproxy/haproxy.sock Backend is using a static LB algorithm and only accepts weights '0%' and '100%'. [root@haproxy ~]# echo "set weight web_host/10.0.0.27 0" | socat stdio /var/lib/haproxy/haproxy.sock [root@haproxy conf.d]# echo "get weight web_host/10.0.0.27" | socat stdio /var/lib/haproxy/haproxy.sock 0 (initial 1)
Haproxy高级功能
基于cookie的会话保持
cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址
hash 调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少, 已经被
session共享服务器代替
注意:不支持 tcp mode,使用 http mode
配置选项
cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [ preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ] name: #cookie 的key名称,用于实现持久连接 insert: #插入新的cookie,默认不插入cookie indirect: #如果客户端已经有cookie,则不会再发送cookie信息 nocache: #当client和haproxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie, 因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器
配置案例
listen web_portbind 10.0.0.7:80balance roundrobinmode http #不支持 tcp modelog globalcookie WEBSRV insert nocache indirectserver web1 10.0.0.17:80 check inter 3000 fall 2 rise 5 cookie web1server web2 10.0.0.27:80 check inter 3000 fall 2 rise 5 cookie web2
ACL基础
访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。
ACL配置选项
acl <aclname> <criterion> [flags] [operator] [<value>] acl 名称 匹配规则 匹配模式 具体操作符 操作对象类型
ACL-Name
acl image_service hdr_dom(host) -i img.openlab.com #ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如Image_site和image_site就是两个完全不同的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.openlab.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 block if bad_agent #有些功能是类似的,比如以下几个都是匹配用户请求报文中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 openlab 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 dst #目标IP dst_port #目标PORT src #源IP src_port #源PORT #示例: acl invalid_src src 10.0.0.100 192.168.1.0/24 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匹配方法 -n 不做DNS解析 -u 禁止acl重名,否则多个同名ACL匹配或关系
ACL-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进 行匹配 - subdir match (-m dir) :查看提取出来的用斜线分隔(“/”)的字符串,如其中任一个匹配,则 ACL进行匹配 - domain match (-m dom) :查找提取的用点(“.”)分隔字符串,如果其中任何一个匹配,则ACL进 行匹配
ACL-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.openlab.com exact –精确比较 substring—子串 suffix-后缀比较 prefix-前缀比较 subdir-路径, /wp-includes/js/jquery/jquery.js domain-域名,www.openlab.com - regular expression #正则表达式 - hex block #16进制
ACL调用方式
与:隐式(默认)使用 或:使用“or” 或 “||”表示 否定:使用 "!" 表示 #示例: if valid_src valid_port #与关系,A和B都要满足为true,默认为与 if invalid_src || invalid_port #或,A或者B满足一个为true if ! invalid_src #非,取反,A和B哪个也不满足为true
ACL案例
ACL示例-域名匹配
[root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg frontend openlab_http_portbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl pc_domain hdr_dom(host) -i www.openlab.orgacl mobile_domain hdr_dom(host) -i mobile.openlab.org ###################### acl hosts #################################use_backend pc_hosts if pc_domainuse_backend mobile_hosts if mobile_domaindefault_backend pc_hosts ###################### backend hosts ############################# backend mobile_hostsmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5 backend pc_hostsmode httpserver 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.openlab.org www.openlab.org openlab.org [root@centos6 ~]#curl www.openlab.org 10.0.0.27 [root@centos6 ~]#curl mobile.openlab.org 10.0.0.17 [root@centos6 ~]#curl openlab.org 10.0.0.27
ACL基于源IP或子网调度访问
将指定的源地址调度至指定的web服务器组。
root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg frontend openlab_http_portbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl pc_domain hdr_dom(host) -i www.openlab.orgacl mobile_domain hdr_dom(host) -i mobile.openlab.orgacl ip_range_test src 172.18.0.0/16 10.0.0.6 ###################### acl hosts #################################use_backend pc_hosts if ip_range_test #放在第一行优先生效use_backend pc_hosts if pc_domainuse_backend mobile_hosts if mobile_domaindefault_backend pc_hosts ###################### backend hosts ############################# backend mobile_hostsmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5 backend pc_hostsmode httpserver web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
ACL示例基于源地址的访问控制
拒绝指定IP或者IP范围访问
listen web_hostbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl acl_deny_src src 10.0.0.6 192.168.0.0/24 ###################### acl hosts ##################################block if acl_deny_srchttp-request deny if acl_deny_src #2.1版本后,不再支持block#http-request allowdefault_backend default_web ###################### backend hosts ############################# backend openlab_hostmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5 backend default_webmode httpserver web1 10.0.0.27:80 check inter 2000 fall 3 rise 5
ACL示例匹配浏览器类型
匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组
[root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg frontend openlab_http_portbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl acl_user_agent hdr_sub(User-Agent) -i curl wgetacl acl_user_agent_ab hdr_sub(User-Agent) -i ApacheBench ###################### acl hosts #################################redirect prefix http://10.0.0.8 if acl_user_agent #301临时重定向至新URLhttp-request deny if acl_user_agent_ab #拒绝abdefault_backend pc_hosts ###################### backend hosts ############################# backend mobile_hostsmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5 backend pc_hostsmode httpserver web2 10.0.0.27:80 check inter 2000 fall 3 rise 5
测试界面
[root@centos6 ~]#curl -I 10.0.0.7 #curl -I 命令:-I 选项表示只获取服务器的响应头信息,而不获取响应体内容 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 #curl -L 命令:-L 选项表示跟随重定向。当服务器返回重定向状态码(如 302)时,curl 会自动请求重定向的目标地址。 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 #这里将 User-Agent 设置为 chrome,模拟 Chrome 浏览器发送请求。 10.0.0.27 #模拟ab [root@centos6 ~]#curl -A ApacheBench 10.0.0.7 #模拟 ApacheBench 工具发送请求 <html><body><h1>403 Forbidden</h1> Request forbidden by administrative rules. </body></html> [root@centos6 ~]#ab -n1 -c 1 http://10.0.0.7/ #ab 命令分析: ab 是 Apache HTTP 服务器的性能测试工具,用于对 Web 服务器进行压力测试。 -n1 表示总共发送 1 个请求。 -c 1 表示并发请求数为 1。
ACL示例基于文件后缀名实现动静分离
[root@centos7 ~]#cat /etc/haproxy/conf.d/test.cfg frontend openlab_http_portbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl acl_static path_end -i .jpg .jpeg .png .gif .css .jsacl acl_php path_end -i .php ###################### acl hosts #################################use_backend mobile_hosts if acl_staticuse_backend app_hosts if acl_phpdefault_backend pc_hosts ###################### backend hosts ############################# backend mobile_hostsmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5backend pc_hostsmode httpserver web2 10.0.0.27:80 check inter 2000 fall 3 rise 5 backend app_hostsmode httpserver web2 10.0.0.27:80 check inter 2000 fall 3 rise 5 #分别在后端两台主机准备相关文件 [root@centos17 ~]#ls /var/www/htmlindex.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 openlab_http_portbind 10.0.0.7:80mode httpbalance roundrobinlog globaloption httplog ###################### acl setting ###############################acl acl_static path_beg -i /static /images /javascriptacl acl_static path_end -i .jpg .jpeg .png .gif .css.js ###################### acl hosts #################################use_backend static_hosts if acl_staticdefault_backend app_hosts ###################### backend hosts ############################# backend static_hostsmode httpserver web1 10.0.0.17 check inter 2000 fall 3 rise 5backend app_hostsmode httpserver 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