Hi.
On 18.01.20 13:11, bjun...@gmail.com wrote:
Hi,
i want to redirect the following (the value of the code param should be
rewritten):
abc.de/?v=1&code=1530&b=3 -> abc.de/?v=1&code=6780&b=3
abc.it/?v=2&code=2400&b=2 -> abc.it/?v=2&code=7150&b=2
abc.fr ..
abc.se ..
.
.
When i don't use maps, i can accomplish the task with the following lines (but
this needs many of those lines):
http-request set-header X-Redirect-Url %[url,regsub(code=1530,code=6780,g)] if
{ hdr_reg(host) -i ^abc\.de$ }
http-request set-header X-Redirect-Url %[url,regsub(code=2400,code=7150,g)] if
{ hdr_reg(host) -i ^abc\.it$ }
http-request redirect code 302 location
https://%[hdr(host)]%[hdr(X-Redirect-Url)]
But i want to use map files to reduce duplication and make it easier to add new
items.
I have these map files (domain is the lookup key):
desktop_ids.map:
abc.de 1530
abc.it 2400
.
.
mobile_ids.map:
abc.de 6780
abc.it 7150
.
.
http-request set-header X-ID-Desktop
%[hdr(host),lower,map_str(/etc/haproxy/desktop_ids.map)]
http-request set-header X-ID-Mobile
%[hdr(host),lower,map_str(/etc/haproxy/mobile_ids.map)]
What i would need is the following:
http-request set-header X-Redirect-Url
%[url,regsub(code=%[hdr(X-ID-Desktop)],code=%[hdr(X-ID-Mobile)],g)]
http-request redirect code 302 location
https://%[hdr(host)]%[hdr(X-Redirect-Url)]
But that's not possible, you cannot use variables in the regex or substitution field of regsub.
I've also tried "http-request replace-header", but same problem, you cannot use variables
for the "match-regex".
Maybe is it possible to cut the "code" param from the query string and append
it with the new value to the query string. But this needs some complex regex and handling
of multiple conditions in query string (plus ordering of query string params is different
than).
Is there a possibility to use variables in regsub or in the "match-regex" of
replace-header?
I think you will need a small lua action handler which do the job.
In the blog post are some examples which can help you to reach your target.
https://www.haproxy.com/blog/5-ways-to-extend-haproxy-with-lua/
Maybe you can start with the example on stackoverflow and add the map handling
into the code.
https://stackoverflow.com/questions/50844292/how-to-make-ha-proxy-to-follow-redirects-by-itself/50872246#50872246
https://www.arpalert.org/src/haproxy-lua-api/2.0dev/index.html#map-class
It would be interesting to know which haproxy version do you use.
haproxy -vv
Best regards / Mit freundlichen Grüßen
Bjoern
Hth
Aleks