HAProxy reqrep 未替换 url 中的字符串

HAProxy reqrep not replacing string in url

我们弃用了一些在 http://localhost/test and is now available under http://localhost/app/testing 下可用的软件。我们正在使用 HAProxy 1.4

因此,我想通过 haproxy,将所有包含 /test 的 url 替换为 /app/testing/。我首先尝试使用 redirect prefix 以保留查询字符串,但是 /test 没有从 url 中删除并且有类似 /app/testing/test/?id=x.

的东西
frontend all
    bind  0.0.0.0:80
    timeout client 86400000
    acl is_test path_beg /test
    redirect prefix /app/testing code 301 if is_test

然后用了一个reqrep,貌似是重定向到新软件,但是url里的/test字串一直没被替换

frontend all
    bind  0.0.0.0:80
    timeout client 86400000
    reqrep ^([^\ :]*)\ /test[/]?(.*)  \ /app/testing/

试试这个
这适用于您在 HAProxy 1.6

上的场景
 acl test     path_beg -i /test
 http-request set-header         X-Location-Path %[capture.req.uri] if test
 http-request replace-header     X-Location-Path /test /app/testing if test
 http-request redirect location  %[hdr(X-Location-Path)] if test
 use_backend WHEREVER            if test

由于 url 版本 1.4 无法重写并且我们不想更新 HAProxy,我们继续使用 reqrep 并保持旧的 link 不变

reqrep ^([^\ :]*)\ /test[/]?(.*)  \ /app/testing/

使用 redirect prefix 意味着,按照 HAProxy 示例,更多用于更改主机名,但保留路径。例如,http://apple.com/my/path could be redirected to http://orange.com/my/path 与:

redirect prefix http://orange.com if apple

HAProxy 1.4 docs 说:

With "redirect prefix", the "Location" header is built from the concatenation of < pfx > and the complete URI path

对我来说,这表明您会期望为 "prefix" 放置的任何内容都将作为路径中已有内容的前缀。这解释了您所看到的行为。这对于更改为新域(例如从 apple.comorange.com)但保持原始路径很有用(例如 /my/path)。

您可以切换到使用 redirect location 来替换整个 URL。以下将重定向 http://apple.com/my/path to http://orange.com/path:

redirect location http://orange.com/path if apple

更新:

如果您想更改 URL,但保留查询字符串,请按原样使用 reqirepreqrep 重写 URL这样做,还要把一个redirect prefix改成frontend。 URL 将被重写 然后 用户将被重定向到它,以便他们看到它。

您可以将 "prefix" 设置为“/”。 HAProxy 文档说:

As a special case, if < pfx > equals exactly "/", then nothing is inserted before the original URI. It allows one to redirect to the same URL (for instance, to insert a cookie).

使用您的代码示例,如下所示:

reqrep ^([^\ :]*)\ /test[/]?(.*)  \ /app/testing/
redirect prefix / code 301 if is_test