继续讲解HAProxy的一些进阶配置及用法,包括报文修改,日志配置,压缩功能,后端服务器状态监测及ACL等功能应用。配置环境及配置文件均延续上一篇《企业级应用:负载均衡层——haproxy(一)》,有任何疑问可以先看上一篇博客。
haproxy的进阶配置
haproxy报文修改
在http模式下,基于实际需求修改客户端的请求报文与响应报文,通过reqadd和reqdel在请求报文添加删除字段,通过rspadd与rspidel在响应报文中添加与删除字段。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15在请求报文尾部添加指定首部
reqadd <string> [{ | unless} <cond>]
从请求报文中删除匹配正则表达式的首部
reqdel <search> [{ | unless} <cond>]
reqidel <search> [{ | unless} <cond>]
在响应报文尾部添加指定首部
rspadd <string> [{ | unless} <cond>]
示例:
rspadd -Via:\ HAPorxy
从响应报文中删除匹配正则表达式的首部
rspdel <search> [{ | unless} <cond>]
rspidel <search> [{ | unless} <cond>]
示例:
rspidel server.* #从相应报文删除server信息
rspidel X-Powered-By:.* #从响应报文删除X-Powered-By信息
HAProxy日志
HAProxy日志配置
需在HAProxy和Rsyslog中分别配置。
- HAProxy配置
1
2
3
4
5
6
7在global配置项定义:
log 127.0.0.1 local{1-7} info #基于syslog记录日志到指定设备,级别有(err、warning、info、debug)
listen web_port
bind 127.0.0.1:80
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
然后重启HAProxysystemctl restart haproxy
- Rsyslog配置
编辑配置文件vim /etc/rsyslog.conf
1
2
3
local3.* /var/log/haproxy.log
然后重启Rsyslogsystemctl restart rsyslog
HAProxy日志格式
将特定信息记录在日志中
配置选项:
capture cookie <name> len <length>
#捕获请求和响应报文中的 cookie并记录日志
capture request header <name> len <length>
#捕获请求报文中指定的首部内容和长度并记录日志
capture response header <name> len <length>
#捕获响应报文中指定的内容和长度首部并记录日志
示例:1
2
3capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
配置示例
1 | listen web_host |
压缩功能
启用功能可以对响应给客户端的报文进行压缩,以节省网络带宽,但是会占用部分CPU性能。
配置选项
1 | compression algo #启用http协议中的压缩机制,常用算法有gzip deflate |
配置示例
1 | listen web_host |
后端服务器状态监测
haproxy能对后端服务器状态进行检测,如果发现后端服务器异常,可以自动将该服务器下线,实现高可用。
haproxy对后端服务器有三种检测方式:
- 基于四层的传输端口做状态监测
- 基于指定URI 做状态监测
- 基于指定URI的request请求头部内容做状态监测
1
2
3
4option httpchk
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>
之前我们的配置都是基于传输IP加端口对检测,所以status状态页的后端检测状态里显示的是L4,基于指定URI做状态监测,需要持续从服务器get指定页面,会占用消耗一些带宽资源,所以基于指定URI的request请求头部内容做状态监测最为合理,配置如下:1
2
3
4
5
6
7
8
9
10
11listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
# option httpchk GET /app/monitor/check.html HTTP/1.0
option httpchk HEAD /app/monitor/check.html HTTP/1.0\r\nHost:\ 192.168.7.102
cookie SERVER-COOKIE insert indirect nocache
server web1 192.168.32.81:80 cookie web1 check inter 3000 fall 3 rise 5
server web2 192.168.32.82:80 cookie web2 check inter 3000 fall 3 rise 5
这时再去看9999端口的status页,就会看到后端服务器的检测状态为L7OK了
HAProxy的ACL功能
访问控制列表(ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,允许其通过或丢弃。
ACL配置选项:
1 | acl <aclname> <criterion> [flags] [operator] [<value>] |
ACL-Name
实例:acl image_service hdr_dom(host) -i img.example.com
ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如Image_site和image_site完全是两个acl。
ACL-criterion
定义ACL匹配规范1
2
3
4
5
6
7
8
9
10
11
12hdr([<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>]]):子串匹配
dst 目标IP
dst_port 目标PORT
src 源IP
src_port 源PORT
示例:1
2
3
4
5
6hdr <string>用于测试请求头部首部指定内容
hdr_dom(host) 请求的host名称,如 www.example.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
ACL-flags
ACL匹配模式
-i 不区分大小写
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否则多个同名ACL匹配或关系
ACL-operator
ACL 操作符1
2
3
4
5
6
7
8整数比较: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的类型1
2
3
4
5
6
7
8
9
10
11
12
13The 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.example.com
suffix-后缀比较
prefix-前缀比较
subdir-路径, /wp-includes/js/jquery/jquery.js
domain-域名,www.example.com
regular expression #正则表达式
hex block #16进制
ACL调用方式
ACL调用方式:
- 与:隐式(默认)使用
- 或:使用“or” 或 “||”表示
- 否定:使用“!“ 表示
示例:1
2
3if valid_src valid_port #与关系
if invalid_src || invalid_port #或
if ! invalid_src #非
ACL具体示例
域名匹配:
1 | listen web_host |
匹配浏览器类型:
匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17listen web_host
bind 172.18.32.249:80
mode http
balance roundrobin
log global
option httplog
acl web_host hdr_dom(host) www.example.net
use_backend example_host if web_host
acl redirect_test hdr(User-Agent) -m sub -i "Mozilla/5.0 (Windows NT 6.1; WOW64;Trident/7.0; rv:11.0) like Gecko"
redirect prefix http://192.168.7.103 if redirect_test
default_backend default_web
backend example_host
mode http
server web1 192.168.32.81:80 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web2 192.168.32.82:80 check inter 2000 fall 3 rise 5
基于文件后缀名实现动静分离:
1 | listen web_host |
匹配访问路径实现动静分离:
1 | listen web_host |
预定义ACL
预定义ACL:
ACLname | 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 |
预定义ACL使用示例1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18listen web_host
bind 172.18.32.249: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.32.81 check inter 2000 fall 3 rise 5
backend static_path_host
mode http
server web1 192.168.32.82 check inter 2000 fall 3 rise 5
backend default_web
mode http
server web1 192.168.32.8:80 check inter 2000 fall 3 rise 5
详细信息查看官网http://cbonte.github.io/haproxy-dconv/2.0/configuration.html#7.4