我有一台服务器,它托管多个站点,但从逻辑上看,它们看起来像是子目录。例如:
- http://XXX/Site1
- http://XXX/Site2
如何配置 HAProxy 的健康检查来检查每个“子站点”?
答案1
使用多个后端,每个后端检查 1 个站点。使用 ACL 执行内容切换,即根据 HTTP 请求的某些属性(在本例中为 URL 路径)选择将请求路由到的后端。
例子:
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000
frontend 1.2.3.4-80
bind 1.2.3.4:80
mode http
option http-server-close
option logasap
acl url_site1 url_beg /Site1
acl url_site2 url_beg /Site2
use_backend site1 if url_site1
use_backend site2 if url_site2
backend site1
mode http
balance roundrobin
option httpchk HEAD /Site1 HTTP/1.1\r\nHost:\ XXX
server srv1 1.1.1.1:80 check
server srv2 2.2.2.2:80 check
backend site2
mode http
balance roundrobin
option httpchk HEAD /Site2 HTTP/1.1\r\nHost:\ XXX
server srv1 1.1.1.1:80 check
server srv2 2.2.2.2:80 check
答案2
您可以在 Haproxy 中启用 HTTP 检查来检查 HTTP 服务器
https://code.google.com/p/haproxy-docs/wiki/httpchk
backend cluster1
mode http
balance roundrobin
option httplog
option httpclose #Disable keepalive
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
backend cluster2
mode http
balance roundrobin
option httplog
option httpclose #Disable keepalive
option forwardfor
option httpchk HEAD /check.txt HTTP/1.0
# Servers