HAProxy and reqrep path rewriting with redirect configuration

djool picture djool · Oct 15, 2015 · Viewed 9.1k times · Source

With HA Proxy 1.5 I need to rewrite URL from http://main.domain.com/my-foo to http://othersite.com:8081/other-bar

Here is what I tried:

frontend ft_def
  bind :80                                      
  mode http                                                  
  acl has_special_uri path_beg /my-foo
  use_backend def if has_special_uri
  default_backend def

backend def
  mode http                                       
  option forwardfor                       
  reqirep ^([^\ ]*\ )/my-foo(.*)    \1/other-bar\2
  server myserver othersite.com:8081

This works: URL
http://main.domain.com/my-foo/home.html
becomes
http://othersite.com:8081/other-bar/home.html

and in the browser the initial URL http://main.domain.com/my-foo/home.html appears.

It is exactly what I need: it is completely transparent for the user. But redirect does not work: when I click on a link on the page the URL is then
http://main.domain.com/other-bar/page2.html

I would like to get http://main.domain.com/my-foo/page2.html instead appearing in the browser.

Is it possible with HA Proxy? I tried many configurations without success. Thanks!

Answer

Michael - sqlbot picture Michael - sqlbot · Oct 15, 2015

If you're talking about links in HTML (as opposed to, say, Location: headers for redirects)... HAProxy 1.5 won't be able to modify those.

Presumably, based on what you describe, the page /other-bar/page1.html is internally linking to <a href="/other-bar/page2.html"> when it really should link to <a href="page2.html">. You'd need relative links in order for something like this to work transparently... otherwise, component "X" in your chain will have to be able to modify links on the fly in the response body, but only links, of course, since you wouldn't want to blindly regex-replace the page contents as a whole... and HAProxy 1.5 doesn't munge response bodies, so it can't fulfill the role of component "X."

HAProxy 1.6 might be able to do this, with Lua, but that's a maybe... and if it can be made to do it, it isn't likely to be at the level of performance you'd typically expect from HAProxy, because scrubbing the html in Lua is probably going to be a relatively expensive proposition.