【问题标题】:HAProxy Routing Based On Url基于 URL 的 HAProxy 路由
【发布时间】:2017-12-18 12:28:09
【问题描述】:

场景:如果 url 是 http://domainhttp://domain/web 则不应该有重定向。对于其他任何事情,比如http://domain/abchttp://domain/xyz,应该重定向到http://domain2

这是我尝试过的,但它只适用于http://domain/web。如果 url 是http://domain,它仍然会重定向到http://domain2

acl is_root path -i
acl missing_slash path_reg ^/*/[^/]*$$  
acl isweb path_beg /web
http-request set-header X-Location-Path %[capture.req.uri] if  !isweb !is_root !missing_slash
http-request replace-header X-Location-Path [^/]+/(.*) \1 if  !isweb !is_root !missing_slash
http-request redirect code 301 location https://www.domain2.com%[hdr(X-Location-Path)] if  !isweb !is_root !missing_slash

【问题讨论】:

    标签: redirect haproxy


    【解决方案1】:

    匹配字符串/

    acl is_root path -m str /
    

    请求 URI 在 HTTP 中永远不能是空字符串。无论地址栏是否真的在主机名后面显示/,浏览器都会发送GET / HTTP/1.1

    【讨论】:

    • 很遗憾,这次改动后的结果还是一样
    • 您的missing_slash 测试似乎无效。不确定您要对此做什么,但正则表达式中的 * 表示“零次或多次出现”,因此这似乎与 / 匹配,防止其他规则生效。您可能打算使用path_reg [^/]$——路径不以斜线结尾——或path_reg ^/.+/[^/]+$。请解释该模式应该匹配什么。
    • 我的正则表达式很差。这就是我想知道的......如果一个网址是这样的 www.mydomain.com 或 www.mydomain.com/web。除了这两个条件,重定向将指向 domain2。
    • 从所有三个测试中删除 !missing_slash 测试。根据您的 HAProxy 版本,此过程比您的方法要简单得多。请注明版本(haproxy -v)。
    • HAProxy 版本 1.6.3。我会试试。到目前为止,我已经尝试了许多不同的事情,但都没有成功。除了查找用户是否输入了 www.domain.com 之外的所有操作都有效。
    猜你喜欢
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2014-09-07
    • 2014-12-30
    • 2020-02-28
    • 2019-11-04
    • 1970-01-01
    • 2023-01-13
    相关资源
    最近更新 更多