HAProxy switch to backup on 500 errors and/or server down

I'm setting up an HAProxy in front of only 2 backend servers, with a particular configuration: any request should go to server A; but, if server A returns a 5xx error code, all request should go to a backup server B. When A returns "up", all the requests should go to A.

I'm trying this configuration:

backend example_cluster
        balance roundrobin
        option httpclose
        option forwardfor

        option httpchk HEAD /ping.html HTTP/1.0\r\nHost:www.example.com
        http-check disable-on-404
        default-server error-limit 1 on-error mark-down

        redirect scheme https if !{ ssl_fc }
        server node1 1.2.3.4:80 check observe layer7
        server node_back 5.6.7.8:443 backup ssl verify none

But it doesn't work for two reasons:

  1. All requests are routed to server node_back (B), even if node1 (A) is up.
  2. It seems that no httpchecks are performed against server A; or better, in syslog I don't see any error regarding the server A down.

If I remove the "option httpchk" line, and the two lines just below that; and I remove also the "observe layer7" in server A; HAProxy works by routing all requests to node A. But, obviously, when the server A returns a 500, HAProxy does not switch to B. So, I'm assuming that the problem might be in the option httpchk configuration.


Solution 1:

From the official documentation: Active Passive Load Balancing With HAProxy

defaults
  mode http
  option http-server-close
  timeout client 20s
  timeout server 20s
  timeout connect 4s

frontend ft_app
  bind 10.0.0.100:80 name app
  default_backend bk_app

backend bk_app
  server s1 10.0.0.1:80 check
  server s2 10.0.0.2:80 check backup