首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HAProxy 1.6使用http-响应、地图和regsub重定向

HAProxy 1.6使用http-响应、地图和regsub重定向
EN

Server Fault用户
提问于 2016-10-07 06:19:15
回答 1查看 7.6K关注 0票数 2

我想设置一个类似于这个https://stackoverflow.com/questions/23001799/how-do-i-used-the-map-feature-in-haproxy-to-build-massive-redirect-tables-1-5的重定向映射

区别在于我想使用http-response而不是http-request。原因是我只想在后端服务器返回404时重定向。

这是我的配置

代码语言:javascript
复制
http-response redirect location %[capture.req.uri,regsub(\?(.*),),map(/etc/haproxy/redirects.map)] code 301 if { status 404 } { capture.req.uri,regsub(\?(.*),),map(/etc/haproxy/redirects.map) -m found }

我尝试使用regsubcapture.req.uri中删除查询参数。但是,当重新启动HAProxy时,我会得到这个错误。

[ALERT] 280/171612 (6176) : parsing [/etc/haproxy/haproxy.cfg:87] : error detected in proxy 'http' while parsing 'http-response redirect' rule : error in condition: invalid arg 2 in conv method 'regsub' : missing arguments (got 1/2), type 'string' expected in ACL expression 'capture.req.uri,regsub(\?(.*),),map(/etc/haproxy/redirects.map)'. [ALERT] 280/171612 (6176) : Error(s) found in configuration file : /etc/haproxy/haproxy.cfg Errors found in configuration file, check it with 'haproxy check'.

有没有一种不需要查询参数来获取URL的方法?我尝试使用path而不是capture.req.uri,但是HAProxy不会启动。

这是我使用path http-response redirect location %[path,map(/etc/haproxy/redirects.map)] code 303 if { status 404 } { path,map(/etc/haproxy/redirects.map) -m found }的配置

这就是警告

[WARNING] 283/090721 (2875) : parsing [/etc/haproxy/haproxy.cfg:88] : 'redirect' : sample fetch <path,map(/etc/haproxy/redirects.map)> may not be reliably used here because it needs 'HTTP request headers' which is not available here. [WARNING] 283/090721 (2875) : parsing [/etc/haproxy/haproxy.cfg:88] : anonymous acl will never match because it uses keyword 'path' which is incompatible with 'backend http-response header rule'

EN

回答 1

Server Fault用户

回答已采纳

发布于 2016-10-10 02:22:10

最初的问题是regsub(\?(.*),)的一个问题,这导致了一个问题,因为regsub转换器仅限于配置解析器可以处理的表达式--而且括号是不可用的,因为解析器将)看作是以太少的参数结束regsub()。(对于文本,您可以使用\\xnn十六进制转义来绕过解析器的限制,但在这里不起作用。)

使用regsub是因为这个重定向是在响应处理if { status 404 }期间触发的,而path提取在处理的那个阶段是不可用的--当请求发送到服务器时,HAProxy释放了请求所使用的缓冲区。

但是,HAProxy 1.6还引入了用户变量,如果在事务处理(txn)作用域中使用,这些变量可用于从请求端传输数据。

在请求处理期间,将path fetch的内容存储在一个称为(巧合地) path的事务作用域变量中。

代码语言:javascript
复制
http-request set-var(txn.path) path

然后,可以在响应处理期间访问它。

为了清晰起见,在多行上显示了下面的内容,但必须是在一条配置行上。

代码语言:javascript
复制
http-response redirect 
    location %[var(txn.path),map(/etc/haproxy/redirects.map)] 
    code 303 
    if { status 404 } { var(txn.path),map(/etc/haproxy/redirects.map) -m found }

这--如果响应状态代码是404 --从变量中提取值并检查它在映射文件中是否有一个值。如果是,则该值用于重定向。

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/807642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档