配置 HAProxy 以根据 url_param 路由流量

Configuring HAProxy to route traffic based on url_param

我是 HAProxy 的新手。我花了几个小时试图弄清楚如何去做,但找不到任何线索。我的要求是:

如果请求的终点是 /special 那么我需要检查 URL_PARAM。

For example: localhost/special?id=10

根据 ID,我需要将其路由到 3 个服务器之一。 If id <=3 server1, if id > 3 and id <=6 server2 else server3.

如果终点不是所有 3 个服务器之间的特殊循环。

如何实现这 2 个级别的平衡?

您可以使用 urlp and urlp_val to extract the id. Then, use acl to match the integer:

acl is_special     path_beg /special
acl small_id         urlp_val(id) le 3
acl medium_id        urlp_val(id) 4:6
acl high_id          urlp_val(id) gt 6

use_backend bck1     if is_special small_id
use_backend bck2     if is_special medium_id
use_backend bck3     if is_special high_id
default_backend      bck_all

然后,创建 3 个后端:每个案例一个。

编辑:

如果要在查询参数上使用正则表达式,请使用 urlp_reg:

acl small_id       urlp_reg(id) ^[0-3]
acl medium_id      urlp_reg(id) ^[4-6]
acl high_id        urlp_reg(id) ^[7-9]

同时检查 stick,具体取决于您要执行的操作。