我只在两台后端服务器前面设置了一个HAProxy,并configuration了一个特定的configuration:任何请求都应该到达服务器A; 但是,如果服务器A返回一个5xx错误代码,则所有的请求都应该到达一个备份服务器B.当A返回“up”时,所有的请求都将转到A.
我正在尝试这个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
但是这不起作用有两个原因:
如果我删除“选项httpchk”行,并在下面的两行; 我也删除服务器A中的“观察层7”; HAProxy通过将所有请求路由到节点A工作。但显然,当服务器A返回500时,HAProxy不会切换到B.所以,我假设问题可能出在httpchkconfiguration选项中。
顺便说一句,我发现我的haproxy pre-nginxconfiguration,我想你应该试试看:
frontend foo bind 192.168.0.1:9080 option httpchk default_backend bar-web backend bar-web mode http balance roundrobin server bar1 192.168.1.2:9080 check observe layer4 weight 50 server bar2 192.168.1.3:9080 check observe layer4 weight 50
从官方文档: 使用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