1. 程式人生 > 實用技巧 >apache配置健康檢查

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判斷為正常,否則為異常