'Rewrite url in haproxy
I have haproxy as front layer to our nodejs app and I'm looking for a way to rewrite url. For example, if customers go to https://aaa.com/product/123, haproxy will rewrite to url https://bbb.com/product/123 . is this possible to do in Haproxy? The important is we want to preserve the url parameter (product/123) and just change the host name
Solution 1:[1]
You say you want to "rewrite" but that is a term that is often misused. What is your intention?
- Do you want to rewrite the incoming URL and then change the address in the browser's address bar with an HTTP redirect?
If so, in proxy configuration:
http-request redirect prefix https://example.org if { hdr(host) -i example.com }
This changes example.com
to example.org
and tells the browser to ask again.
Test:
curl -v https://example.com/foo/bar/1234?query=yes
...
< HTTP/1.1 302 Found
< Cache-Control: no-cache
< Content-length: 0
< Location: https://example.org/foo/bar/1234?query=yes
< Connection: close
This is the simplest solution if it fits your need, because the net result is that the browser is actually making the correct request itself, reducing the potential for unexpected behavior.
- Or do you want to change the
Host:
header that the backend server sees, but not send a redirect, and leave the browser's address bar as it was?
This changes example.com
to example.org
in the Host:
header that the back-end server sees in the request from HAProxy.
http-request set-header Host example.org if { hdr(host) -i example.com }
This will do exactly what is intended, but it may not have the desired result, particularly if the application is aware of other inconsistencies, such as the incoming Referer:
or Origin:
being inconsistent with the Host:
, or if it's doing non-portable things with cookies, in which case further header rewriting (possibly in both directions) or application changes may be necessary.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael - sqlbot |