Shawn
Shawn

Reputation: 2819

HaProxy failover based on http status

Is it possible to have HaProxy failover when it encounters a certain http-status codes?

I have the following generic haproxy code that works fine if the tomcat server itself stops/fails. However I would like to fail-over when http-status codes 502 Bad Gateway or 500 Internal Server Error are also encountered from tomcat. The following configuration will continue to send traffic even when 500, 404 status codes are encountered in any node.

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

Thanks In Advance

Upvotes: 2

Views: 4910

Answers (1)

Shawn
Shawn

Reputation: 2819

I found the following HaProxy http-check expect to resolve the load-balancing based on http status codes.

# 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

In order to fail-over when a 500 error is encountered, the HaProxy configuration would look like:

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

Source https://cbonte.github.io/haproxy-dconv/configuration-1.5.html#http-check%20expect

Upvotes: 7

Related Questions