HAProxy and reqrep path rewriting with redirect configuration

Multi tool use
HAProxy and reqrep path rewriting with redirect configuration
With HA Proxy 1.5 I need to rewrite URL fromhttp://main.domain.com/my-foo
tohttp://othersite.com:8081/other-bar
http://main.domain.com/my-foo
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-bar2
server myserver othersite.com:8081
This works:
URLhttp://main.domain.com/my-foo/home.html
becomeshttp://othersite.com:8081/other-bar/home.html
http://main.domain.com/my-foo/home.html
http://othersite.com:8081/other-bar/home.html
and in the browser the initial URL http://main.domain.com/my-foo/home.html
appears.
http://main.domain.com/my-foo/home.html
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 thenhttp://main.domain.com/other-bar/page2.html
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.
http://main.domain.com/my-foo/page2.html
Is it possible with HA Proxy? I tried many configurations without success.
Thanks!
2 Answers
2
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.
Location:
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."
/other-bar/page1.html
<a href="/other-bar/page2.html">
<a href="page2.html">
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.
Normally any URL changes should be handled by the web app itself, e.g. there is a :path
option under :url
configuration in Phoenix.Endpoint
for the Phoenix framework: https://hexdocs.pm/phoenix/Phoenix.Endpoint.html, which was specifically designed for this purpose. Then the browser will still request the URL appears in the HTML file but HAProxy will always rewrite it.
:path
:url
Phoenix.Endpoint
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.