apache配置健康检查

 

配置负载均衡时需要根据后端服务的情况来决定是否将请求代理过去。这时候就需要做健康检查。

apache主要有三种方式对代理对象做检查

HTTP GET请求方式

HTTP HEAD 方式

TCP 方式

下面介绍http get 和 tcp的配置方式

具体可以参考官方文档http://httpd.apache.org/docs/2.4/mod/mod_proxy_hcheck.html

http get

首先配置检查表达式如

ProxyHCExpr ok234 {%{REQUEST_STATUS} =~ /^[234]/}
ProxyHCExpr gdown {%{REQUEST_STATUS} =~ /^[5]/}
ProxyHCExpr in_maint {hc('body') !~ /Under maintenance/}

然后在代理对象后面配置来使用这些检查的表达式

BalancerMember "http://localhost:8764" hcmethod=GET hcexpr=ok234 hcuri=/sayHello

BalancerMember "http://localhost:8765" hcmethod=GET hcexpr=ok234 hcuri=/sayHello

按照上面配置则意味着,我要通过访问sayHello接口来判断存活状态

hcexpr代表检查的表达式,ok234,则意味着,后端返回的http status为2xx,3xx,4xx。为成功,否则为失败

如果按照以下配置

BalancerMember "http://localhost:8764" hcmethod=GET hcexpr=in_maint hcuri=/sayHello

BalancerMember "http://localhost:8765" hcmethod=GET hcexpr=in_maint hcuri=/sayHello

则意味着,当请求接口的body中不包含Under maintenance判断为正常,否则为异常

原文地址:https://www.cnblogs.com/liguangming/p/13502841.html