企业级应用:负载均衡层——haproxy(二)

本文首发于我的个人网站: https://hewanyue.com/
本文作者: Hechao
本文链接: https://hewanyue.com/blog/f5daec50.html

haproxy的进阶配置

haproxy报文修改

  在http模式下,基于实际需求修改客户端的请求报文与响应报文,通过reqadd和reqdel在请求报文添加删除字段,通过rspadd与rspidel在响应报文中添加与删除字段。

在请求报文尾部添加指定首部
    reqadd <string> [{if | unless} <cond>]
从请求报文中删除匹配正则表达式的首部
    reqdel <search> [{if | unless} <cond>]
    reqidel <search> [{if | unless} <cond>]
在响应报文尾部添加指定首部
    rspadd <string> [{if | unless} <cond>]
示例:
    rspadd X-Via:\ HAPorxy
从响应报文中删除匹配正则表达式的首部
    rspdel <search> [{if | unless} <cond>]
    rspidel <search> [{if | unless} <cond>]
示例:
    rspidel server.* #从相应报文删除server信息
    rspidel X-Powered-By:.* #从响应报文删除X-Powered-By信息

HAProxy日志

HAProxy日志配置

  需在HAProxy和Rsyslog中分别配置。

  • HAProxy配置
在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

$ModLoad imudp
$UDPServerRun 514
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> #捕获响应报文中指定的内容和长度首部并记录日志
  示例:

capture request header Host len 256
capture request header User-Agent len 512
capture request header Referer len 15
配置示例
listen web_host
    bind 172.18.32.249:80
    mode http
    balance roundrobin
    log global
    option httplog #日志格式选项
    capture request header X-Forwarded-For len 15
    capture request header User-Agent len 512
    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

压缩功能

  启用功能可以对响应给客户端的报文进行压缩,以节省网络带宽,但是会占用部分CPU性能。

配置选项
compression algo #启用http协议中的压缩机制,常用算法有gzip deflate
    identity #调试使用的压缩方式
    gzip #常用的压缩方式,与各浏览器兼容较好
    deflate #有些浏览器不支持
    raw-deflate #新出的压缩方式
compression type #要压缩的文件类型
配置示例
listen web_host
    bind 172.18.32.249:80
    mode http
    balance roundrobin
    log global
    option httplog
#    capture request header X-Forwarded-For len 15
#    capture request header User-Agent len 512
    compression algo gzip
    compression type compression type text/plain text/html text/css text/xml text/javascript application/javascript
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

后端服务器状态监测

  haproxy能对后端服务器状态进行检测,如果发现后端服务器异常,可以自动将该服务器下线,实现高可用。
  haproxy对后端服务器有三种检测方式:

  • 基于四层的传输端口做状态监测
  • 基于指定URI 做状态监测
  • 基于指定URI的request请求头部内容做状态监测
option httpchk
option httpchk <uri>
option httpchk <method> <uri>
option httpchk <method> <uri> <version>

  之前我们的配置都是基于传输IP加端口对检测,所以status状态页的后端检测状态里显示的是L4,基于指定URI做状态监测,需要持续从服务器get指定页面,会占用消耗一些带宽资源,所以基于指定URI的request请求头部内容做状态监测最为合理,配置如下:

listen 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配置选项:

acl <aclname> <criterion> [flags] [operator] [<value>]
acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型
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匹配规范

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>]]):子串匹配
dst 目标IP
dst_port 目标PORT
src 源IP
src_port 源PORT

  示例:

hdr <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 操作符

整数比较: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
      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” 或 “||”表示
  • 否定:使用“!“ 表示

  示例:

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

ACL具体示例

域名匹配:
listen 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
    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
匹配浏览器类型:

  匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组

listen 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
基于文件后缀名实现动静分离:
listen web_host
    bind 172.18.32.249: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.32.81 check inter 2000 fall 3 rise 5
backend image_server_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
匹配访问路径实现动静分离:
listen 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 static_path
    default_backend default_web
backend static_path_host
    mode http
    server web1 192.168.32.81 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
预定义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使用示例

listen 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

发布了43 篇原创文章 · 获赞 52 · 访问量 8602

猜你喜欢

转载自blog.csdn.net/MicePro/article/details/102969034