Haproxy更改url的一部分

我有一个应用程序被重命名,我希望Haproxyredirect到正确的path,同时保留请求参数

这是我的:

acl old_name path_dir -i /old_name http-request set-path /new_name/%[query] if old_name 

我想从它改变

 www.site.com/old_name/Default.aspx?Id=123 

 www.site.com/new_name/Default.aspx?Id=123 but this is not working. 

使用HAProxy 1.5:使用临时头从请求中的现有头创build新path,然后直接执行redirect

 # Clean the request and remove any existing header named X-Rewrite http-request del-header X-REWRITE # Copy the full request URL into X-Rewrite unchanged http-request add-header X-REWRITE %[url] if { path_beg /old_path } # Change the X-REWRITE header to contain out new path http-request replace-header X-REWRITE ^/old_path(/.*)?$ /new_path\1 if { hdr_cnt(X-REWRITE) gt 0 } # Perform the 301 redirect http-request redirect code 301 location http://%[hdr(host)]%[hdr(X-REWRITE)] if { hdr_cnt(X-REWRITE) gt 0 } 

在HAProxy 1.6中,使用regsubfilter

 http-request redirect code 301 location http://%[hdr(host)]%[url,regsub(^/old_path,/new_path,)] if { path_beg /old_path } 

来源之间的其他有用的configuration片段

您正在将urlredirecturl重写混淆到后端。

如果你甚至想重写 ,那么根据haproxy 1.6文档:

  • “set-path”用格式string的评估结果重写请求path。 查询string(如果有的话)保持不变。

所以在这种情况下 ,正确的configuration是:

 acl old_name path_dir -i /old_name http-request set-path /new_name if old_name 

redirect用户:

 redirect location /new_name if old_name