遇到某些http状态代码时是否可以进行HaProxy故障转移?
如果tomcat服务器本身停止/失败,我有以下通用haproxy代码可以正常工作。但是,当我从tomcat遇到http-status代码 502 Bad Gateway 或 500 Internal Server Error 时,我想要进行故障转移。即使在任何节点中遇到500,404状态代码,以下配置仍将继续发送流量。
backend db01_replication
mode http
bind 192.168.0.1:80
server app1 10.0.0.19:8080 check inter 10s rise 2 fall 2
server app2 10.0.0.11:8080 check inter 10s rise 2 fall 2
server app3 10.0.0.13:8080 check inter 10s rise 2 fall 2
提前致谢
答案 0 :(得分:2)
我发现以下HaProxy http-check expect 可以根据http状态代码解决负载均衡问题。
# Only accept status 200 as valid
http-check expect status 200
# Consider SQL errors as errors
http-check expect ! string SQL\ Error
# Consider all http status 5xx as errors
http-check expect ! rstatus ^5
为了在遇到500错误时进行故障转移,HaProxy配置如下所示:
backend App1_replication
mode http
bind 192.168.0.1:80
http-check expect ! rstatus ^5
server app1 10.0.0.19:8080 check inter 10s rise 2 fall 2
server app2 10.0.0.11:8080 check inter 10s rise 2 fall 2
server app3 10.0.0.13:8080 check inter 10s rise 2 fall 2
来源 https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#http-check%20expect