基于 http 状态的 HaProxy 故障转移

HaProxy failover based on http status

HaProxy 是否可以在遇到特定的 http 状态代码时进行故障转移?

如果 tomcat 服务器本身 stops/fails,我有以下通用的 haproxy 代码可以正常工作 。但是,当 tomcat 也遇到 http 状态代码 502 Bad Gateway500 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

提前致谢

我发现以下 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