HAProxy 在地图查找之前操作字符串
HAProxy manipulate string prior to map lookup
我是 HAProxy 的新手,但有一个看似相当不常见的需求。我需要根据主机 header 在地图中查找项目,但我需要先应用一些字符串操作。
示例:
i.domain.com 收到请求
我需要去掉 i.并在我的地图中查找 domain.com。
我可以通过使用初始请求的值创建一个新的临时 header 然后用一些正则表达式替换该值来做到这一点,如下所示:
http-request set-header X-Temp %[req.hdr(host)]
http-request replace-value X-Temp [a-zA-Z].(.*)(:)?.*
http-request set-header X-ID %[req.hdr(X-Temp),lower,map(/some.map,99999)]
当我真正需要的似乎是这样的 one-liner 时,这似乎很浪费:
http-request set-header X-ID %[(apply [a-zA-Z].(.*)(:)?.* to req.hdr(host)),lower,map(/some.map,99999)]
我曾尝试让 reqrep 执行此操作,但没有任何运气。如何在不实际将字符串保存回 http 请求 headers?
的情况下仅为查找操作字符串
我本来想说 "Lua" :) 但是 HAProxy 1.6 有一个新的 regsub
转换器,它应该可以满足您的需求。
regsub(<regex>,<subst>[,<flags>])
Applies a regex-based substitution to the input string. It does the same operation as the well-known "sed" utility with "s/<regex>/<subst>/
". By default it will replace in the input string the first occurrence of the largest part matching the regular expression <regex>
with the substitution string <subst>
. It is possible to replace all occurrences instead by adding the flag "g" in the third argument <flags>
. It is also possible to make the regex case insensitive by adding the flag "i" in <flags>
. Since <flags>
is a string, it is made up from the concatenation of all desired flags. Thus if both "i" and "g" are desired, using "gi" or "ig" will have the same effect. It is important to note that due to the current limitations of the configuration parser, some characters such as closing parenthesis or comma are not possible to use in the arguments. The first use of this converter is to replace certain characters or sequence of characters with other ones.
reqrep
不起作用,因为它在流程中的处理时间比 http-request
.
晚得多
我是 HAProxy 的新手,但有一个看似相当不常见的需求。我需要根据主机 header 在地图中查找项目,但我需要先应用一些字符串操作。
示例: i.domain.com 收到请求 我需要去掉 i.并在我的地图中查找 domain.com。
我可以通过使用初始请求的值创建一个新的临时 header 然后用一些正则表达式替换该值来做到这一点,如下所示:
http-request set-header X-Temp %[req.hdr(host)]
http-request replace-value X-Temp [a-zA-Z].(.*)(:)?.*
http-request set-header X-ID %[req.hdr(X-Temp),lower,map(/some.map,99999)]
当我真正需要的似乎是这样的 one-liner 时,这似乎很浪费:
http-request set-header X-ID %[(apply [a-zA-Z].(.*)(:)?.* to req.hdr(host)),lower,map(/some.map,99999)]
我曾尝试让 reqrep 执行此操作,但没有任何运气。如何在不实际将字符串保存回 http 请求 headers?
的情况下仅为查找操作字符串我本来想说 "Lua" :) 但是 HAProxy 1.6 有一个新的 regsub
转换器,它应该可以满足您的需求。
regsub(<regex>,<subst>[,<flags>])
Applies a regex-based substitution to the input string. It does the same operation as the well-known "sed" utility with "
s/<regex>/<subst>/
". By default it will replace in the input string the first occurrence of the largest part matching the regular expression<regex>
with the substitution string<subst>
. It is possible to replace all occurrences instead by adding the flag "g" in the third argument<flags>
. It is also possible to make the regex case insensitive by adding the flag "i" in<flags>
. Since<flags>
is a string, it is made up from the concatenation of all desired flags. Thus if both "i" and "g" are desired, using "gi" or "ig" will have the same effect. It is important to note that due to the current limitations of the configuration parser, some characters such as closing parenthesis or comma are not possible to use in the arguments. The first use of this converter is to replace certain characters or sequence of characters with other ones.
reqrep
不起作用,因为它在流程中的处理时间比 http-request
.