我想设置一个类似于这个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时redirect。
这是我的configuration
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 }
我试图使用regsub从capture.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不会启动。
这是我的configuration使用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'
原始问题是regsub(\?(.*),)一个问题,由于regsub转换器仅限于configurationparsing器可以处理的expression式而导致了一个问题,并且括号不可用,因为parsing器看到了)作为closuresregsub()与太less的参数。 (对于文字,您可以使用\\xnnhex转义来解决parsing器的限制,但这不起作用。)
regsub被使用,因为在响应处理期间, if { status 404 }和path获取在该阶段处理中不可用, if { status 404 }在响应处理期间触发该redirectregsub一旦将请求发送到服务器就释放该缓冲器使用的缓冲区。
但是,如果在事务( txn )范围内使用,HAProxy 1.6还引入了可用于从请求端传输数据的用户variables。
在请求处理期间,将path获取的内容存储在称为(一致) path的事务范围variables中。
http-request set-var(txn.path) path
然后,在响应处理期间可以访问它。
为清楚起见,在多行上显示以下内容,但必须在单行configuration中。
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,则将该值从variables中取出,并检查其是否在映射文件中有值。 如果是这样,则该值用于redirect。