HAProxy - 添加尾部斜线

HAProxy - Add Trailing Slash

我正在做一个项目,我需要将发往特定页面的请求路由到单独的后端。

例如,所有对 https://mycooldomain.com will go to backend "A". But, if navigating to https://mycooldomain.com/secretpage 的请求我希望它转到后端 "B"。

现在,我可以正常工作了,但是 运行 遇到了一个问题,我需要尾部斜杠才能正常工作。

所以,我需要一种方法来判断请求是否为 https://[whateverhostnameisused]/secretpage redirect to https://[whateverhostnameisused]/secretpage/

这是我目前的配置示例:

frontend f_https
    bind *:443 ssl crt cert.pem
    reqadd X-Forwarded-Proto:\ https

    #define hosts
    acl host_a hdr(host) -i a.mycooldomain.com
    acl host_b hdr(host) -i b.mycooldomain.com
    acl host_c hdr(host) -i c.mycooldomain.com

    #custom acls
    acl secret path_beg -i /secretpage

    #Custom redirects

    ##define backend
    use_backend b_secret if secret
    use_backend b_a if host_a
    use_backend b_b if host_b
    use_backend b_c if host_c

    default_backend b_https

backend b_secret
    server secret 192.168.15.15:5575 check

看来您正在寻找这样的东西:

http-request redirect scheme https drop-query append-slash if { path -m str /secretpage }

如果应用于前端或后端,这应该有效。

http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#4.2-redirect%20scheme

仅需要指定方案,因为语法需要 location | prefix | scheme 之一,而对于其他两个选项,您必须在配置中自行重新组装 URL。


另请注意,reqadd 并未正式弃用,但添加该请求 header 的首选方式如下:

http-request set-header X-Forwarded-Proto https

请注意,未指定 :,header 名称后的 space 不得使用 \ 进行转义。这实现了相同的结果,但它在 HAProxy 中使用了不同的代码路径,并且应该是一种更高效的操作。您将希望尽可能使用 http-requesthttp-response 指令而不是 reqxxxrspxxx,因为它们也更适合更复杂的操作。