问题
I am pretty new to HAProxy but have the need for what seems like a fairly uncommon requirement. I need to lookup an item in a map based on the host header but I need to apply some string manipulation first.
Example: Request comes in for i.domain.com I need to strip off the i. and lookup domain.com in my map.
I can do this by creating a new, temp header with the value from the initial request and then replacing that value with some regex like so:
http-request set-header X-Temp %[req.hdr(host)]
http-request replace-value X-Temp [a-zA-Z].(.*)(:)?.* \1
http-request set-header X-ID %[req.hdr(X-Temp),lower,map(/some.map,99999)]
This seems wasteful when all I really need seems like it could be a one-liner like this:
http-request set-header X-ID %[(apply [a-zA-Z].(.*)(:)?.* \1 to req.hdr(host)),lower,map(/some.map,99999)]
I've tried to get reqrep to do it without any luck. How can I manipulate the string just for the lookup without actually saving that back to the http request headers?
回答1:
I was going to say "Lua" :) but HAProxy 1.6 has a new regsub converter, which should do exactly what you are looking for.
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
wouldn't work, because it is processed much later in the flow than http-request
.
来源:https://stackoverflow.com/questions/33423249/haproxy-manipulate-string-prior-to-map-lookup