0

I'm sort of new to ha-proxy and trying to rewrite the host_header, keeping the sub-domain intact and this is my first attempt to achieve that:

http-request set-var(req.get_sub) req.hdr(host),lower,regsub(\.website\.com$,) if { hdr_end(host) -i .website.com }
http-request set-header Host %[var(req.get_sub)].company.net if { var(req.get_sub) -m found } 
.....

It's working, to some extent with one issue: it's rewriting the header to demo.company.net just fine when the URL is like demo.website.com. But it returns the original header, if the URL is just website.com, instead of company.net. I understand that it's probably doing the correct thing according to my config but cannot figure out how to make that conditional. Can anyone point me to the right direction pls?

Another thing I wanted to ask: If there is way to define a variable for website.com and use that variable afterwards? Really appreciate any helps/pointer on that.

Best!

MacUsers
  • 469
  • 2
  • 7
  • 13
  • Question: The variable you're creating is not really needed for this, so do you actually need this variable for something else, or did you capture the variable because it happened to be part of something you figured out, that works? – Michael - sqlbot Jan 28 '19 at 23:23
  • I do not need that variable for anything else, other than prepend that to the custom/rewritten header, like: `abc.myweb.com => abc.company.net` or `www.myweb.com => www.company.net`. I just came up with that for various post in the web but If that can be done without doing any of those things, I’d very interested to know how. My only other issue is I must need to do handle the case when it’s only `myweb.com` and wand to rewtite it to `company.net` or `www.company.net` - either way works for me. Thanks. @Michael-sqlbot! – MacUsers Jan 29 '19 at 00:57
  • 1
    Untested, but you might try... `http-request set-header Host %[req.hdr(host),lower,regsub(website\.com$,company.net)] if { hdr_reg(host) -i (^|\.)website.com }` – Michael - sqlbot Jan 29 '19 at 01:10
  • thanks @Michael-sqlbot! That worked straight away. One follow up question though: I actually need to use a number of `acl`s and `http-request` which includes using _website.com_ many times. Is it possible to put that in a variable (using `set-var` or something similar) and use that variable, instead of using literal string? Thanks! – MacUsers Feb 03 '19 at 14:18
  • Can you give examples? In the case above, you wouldn't be able to use a variable inside `regsub()` or `hdr_reg` but depending on your specific needs, you could potentially use the [`map` converter](http://cbonte.github.io/haproxy-dconv/1.6/configuration.html#7.3.1-map). Searching patterns from a file sounds intuitively terrible but HAProxy loads the file into memory at startup and these perform exceptionally. – Michael - sqlbot Feb 03 '19 at 15:08
  • A potential gotcha that catches some users off-guard is the way HAProxy actually evaluates ACLs -- the conditions in an ACL are (re-)evaluated each time the ACL is tested. If you test an ACL that references the host header, and it matches, so rewrite the host header such that it no longer matches the ACL... and later have another configuration directive that tests the ACL a second time, it will evaluate as false on the second test, because it is "no longer" true. That was why I asked, before, whether you needed the variable for anything else. – Michael - sqlbot Feb 03 '19 at 15:13

0 Answers0