gpt4 book ai didi

haproxy - 使用 HAProxy 重定向 URL 路径并保留查询字符串

转载 作者:行者123 更新时间:2023-12-05 09:17:00 26 4
gpt4 key购买 nike

我们使用以下方法将 URL 映射文件加载到 HAProxy 中:

http-request redirect location %[capture.req.uri,map(/etc/haproxy/redirects.map)] code 301 if { capture.req.uri,map(/etc/haproxy/redirects.map) -m found }

map 文件中一行的例子是:

/shop-by-category /products

此重定向按预期工作。我遇到的唯一问题是使用/shop-by-category 路径传递查询字符串时。例如:

/shop-by-category?testingquerystring=test

这不会重定向。

我尝试将我认为是 HAProxy 查询变量的内容添加到重定向映射中,例如:

/shop-by-category&%[query] /parts&%[query]

但是 HAProxy 似乎无法在映射文件中识别这一点。有什么方法可以让 HAProxy 动态识别查询字符串并在重定向时传递它?

最佳答案

我有理由相信您不能在 map 文件中使用提取——它是静态的,并在启动时加载,因此无法插入任何内容。

capture.req.uri指的是 RFC 意义上的 URI:

capture.req.uri : string

This extracts the request's URI, which starts at the first slash and ends before the first space in the request (without the host part). Unlike "path" and "url", it can be used in both request and response because it's allocated.

...所以听起来您应该使用 path获取匹配和 query如果已设置,则获取重写。

path : string

This extracts the request's URL path, which starts at the first slash and ends before the question mark (without the host part).

query : string

This extracts the request's query string, which starts after the first question mark. If no question mark is present, this fetch returns nothing. If a question mark is present but nothing follows, it returns an empty string. This means it's possible to easily know whether a query string is present using the "found" matching method. This fetch is the complement of "path" which stops before the question mark.

然后需要两行匹配和重写。

首先,如果查询字符串存在则重写:

http-request redirect location %[path,map(/etc/haproxy/redirects.map)]?%[query] code 301 if { path,map(/etc/haproxy/redirects.map) -m found } { query -m found }

然后,如果查询字符串不存在则重写:

http-request redirect location %[path,map(/etc/haproxy/redirects.map)] code 301 if { path,map(/etc/haproxy/redirects.map) -m found } ! { query -m found }

您的 map 文件将只有路径。

关于haproxy - 使用 HAProxy 重定向 URL 路径并保留查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49001819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com