HAProxy在地图查找之前操纵字符串 [英] HAProxy manipulate string prior to map lookup

查看:190
本文介绍了HAProxy在地图查找之前操纵字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对HAProxy很新,但需要一个看起来非常罕见的要求。我需要根据主机头查找地图中的项目,但我需要先应用一些字符串操作。

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.

示例:
请求来自我.domain.com
我需要剥离i。并在我的地图中查找domain.com。

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)]

我试图让reqrep做到没有任何运气。如何操作字符串只是为了查找而不实际将其保存回http请求标题?

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?

推荐答案

我打算去说Lua:)但是HAProxy 1.6有一个新的 regsub 转换器,它应该完全符合您的要求。

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>])




将基于正则表达式的替换应用于输入字符串。它使用 s /< regex> /< subst> / 与众所周知的sed实用程序执行相同的操作。默认情况下,它将在输入字符串中替换与正则表达式< regex> 匹配的最大部分的第一次出现,替换字符串< subst> ; 。可以通过在第三个参数< flags> 中添加标记g来替换所有出现的内容。通过在< flags> 中添加标记i,也可以使正则表达式不区分大小写。由于< flags> 是一个字符串,因此它是由所有所需标志的串联组成的。因此,如果需要i和g,则使用gi或ig将具有相同的效果。重要的是要注意,由于配置解析器的当前限制,一些字符(例如右括号或逗号)不可能在参数中使用。首次使用此转换器是将某些字符或字符序列替换为其他字符。

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

reqrep wouldn't work, because it is processed much later in the flow than http-request.

这篇关于HAProxy在地图查找之前操纵字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆