我正在设置 haproxy 来使用多个后端服务器,但希望其中一个服务器处理特定请求:
backend bagend
mode http
balance roundrobin
option httpchk GET / HTTP/1.1\r\nHost:\ www.example.com
use-server app1_00 if { path_beg /frodo }
use-server app1_01 if { path_beg /frodo }
use-server app1_02 if { path_beg /frodo }
use-server app1_03 if { path_beg /frodo }
server app1_00 10.0.0.1:9100 check
server app1_01 10.0.0.1:9101 check
server app1_02 10.0.0.1:9102 check
server app1_03 10.0.0.1:9103 check
server app2_00 10.0.0.2:9100 check
server app2_01 10.0.0.2:9101 check
server app2_02 10.0.0.2:9102 check
server app2_03 10.0.0.2:9103 check
我的 use-server 命令会在 app1 上的所有 4 个后端之间保持平衡吗?还是我需要以其他方式执行此操作?看起来一切都会顺利app1_00
。
答案1
您要做的是让前端的 acl 根据 URL 将请求发送到不同的后端。
frontend shire
bind *:80
mode http
acl path-frodo path_beg /frodo
use backend longbottom if path-frodo
default_backend bagend
backend longbottom
mode http
balance roundrobin
option httpchk GET / HTTP/1.1\r\nHost:\ www.example.com
server app1_00 10.0.0.1:9100 check
server app1_01 10.0.0.1:9101 check
server app1_02 10.0.0.1:9102 check
server app1_03 10.0.0.1:9103 check
backend bagend
mode http
balance roundrobin
option httpchk GET / HTTP/1.1\r\nHost:\ www.example.com
server app1_00 10.0.0.1:9100 check
server app1_01 10.0.0.1:9101 check
server app1_02 10.0.0.1:9102 check
server app1_03 10.0.0.1:9103 check
server app2_00 10.0.0.2:9100 check
server app2_01 10.0.0.2:9101 check
server app2_02 10.0.0.2:9102 check
server app2_03 10.0.0.2:9103 check
所有以 /frodo 开头的请求将仅命中 app1_00、app1_01、app1_02 和 app1_03。所有其他请求将命中所有内容。