Jay Taylor's notes
back to listing indexHow do I match a wildcard host in ACL lists in HAproxy? - Server Fault
[web search]-
Teams
Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat.
Learn more Explore Teams -
Looking for
I have the following lines in my haproxy.conf:
acl valid_domains hdr(Host) -i mysite.com images.mysite.com docs.mysite.com admin.mysite.com
redirect location http://mysite.com/invalid_domain if !valid_domains
How do I match any subdomain?
I tried:
acl valid_domains hdr(Host) -i *.mysite.com
and:
acl valid_domains hdr(Host) -i [a-z]+.mysite.com
... But neither worked.
Thanks
4 Answers
I feel that hdr_sub is better for your needs. I was using hdr_end for a while but it runs into the following problem:
requests with port 80 usually get the port stripped so the host header looks like "example.com", but if you were requesting on a port explicitly, like example.com:8080, the header will have the port, and hdr_end will fail the check for "example.com".
hdr_sub will do a substring match, which seems like a better fit for you (and me).
Either solution still has a nasty thing I don't like. Order dependent evaluation of the results.
e.g (my conditions look like this on the frontend)
acl is_dbadmin hdr_sub(host) -i dbadmin.example.com
Requesting on port 8080 would be like this:
Jul 9 02:48:40 localhost haproxy[8]: 192.168.1.1:55870 [09/Jul/2015:02:48:40.865] http-in example/s1 1/0/0/20/110 200 330722 - - ---- 0/0/0/0/0 0/0 {**example.com:8080**||http://example.com:} {Apache/2.4.10 (Debia||||} "GET /wp-includes/js/zxcvbn.min.js HTTP/1.1"
where as port 80 could likely be like this
Jul 9 02:48:40 localhost haproxy[8]: 192.168.1.1:55870 [09/Jul/2015:02:48:40.865] http-in example/s1 1/0/0/20/110 200 330722 - - ---- 0/0/0/0/0 0/0 {example.com||***http://example.com***:} {Apache/2.4.10 (Debia||||} "GET /wp-includes/js/zxcvbn.min.js HTTP/1.1"
There are cases where you need to be explicit about this, such as handling redirects for wildcard SSL with multiple levels of subdomains.
Matching end (hdr_end
or -m end
) or substring (hdr_sub
or -m sub
) can have unintended side-effects of matching more than you expect. In many cases this may not really matter, since you don't have traffic for those domains coming to the server, but it doesn't mean it's the technically correct solution.
Using a regular expression is the best way I've found to do explicit matching.
For example, if you want to only match *.example.org
without matching sub.domain.example.org
:
acl valid_domains hdr(host) -m reg -i ^[^.]+.example.org$
If you also want to handle (any) non-standard ports, this can be extended slightly:
acl valid_domains hdr(host) -m reg -i ^[^.]+.example.org(:[0-9]+)?$
The above will match:
test1.example.org
test2.example.org:8080
and will not match:
example.org
two.subs.example.org
myexample.org
test.myexample.org
test.example.org.other.com
-
But I want example.org :-) Probably lots do as they map fred.org to www.fred.org ... No pressure to write a new version ...– blisswebCommented Dec 22, 2021 at 8:36
hdr_end
is what you're looking for. Try this:
acl valid_domains hdr_end(host) -i mysite.com
redirect location http://mysite.com/invalid_domain if !valid_domains
-
what if you have two domains. One called mysite.com and one called notmysite.com? The both end with mysite.com. So the match is not specific enought, right?– SaabCommented Jun 1, 2015 at 18:48
-
@Saab in that case I would write
acl valid_domains hdr(host) -i mysite.com
+acl valid_domains hdr_end(host) -i .mysite.com
, or just useacl valid_domains hdr_dom(host) -i mysite.com
– howanghkCommented Aug 27, 2015 at 17:55 -
Amazingly this doesn't work if there is a trailing port number, like :90, even howanghk 's answer doesn't work, have to use something like @gregmac 's answer with the regex if using a port other than (80/noport).– blisswebCommented Dec 22, 2021 at 8:32
I'd offer yet another solution
acl valid_domains hdr_dom(host) -i mysite.com
acl valid_domains hdr_dom(host) -i -m end .mysite.com
Rules in one acl are combined with or. First one accepts just the top domain, second will accept subdomains. Should not be concerned with port thanks to hdr_dom
I know it's an old question, but I still came here looking. See the docs for full detail.
Your Answer
Not the answer you're looking for? Browse other questions tagged or ask your own question.
- Featured on Meta
-
-
Related
Hot Network Questions
- What is the historical origin of lone CR as a line terminator?
- I found these wild prickly plants on a trip in Cambridge, what are they?
- Does forgetful functor commute with limits?
- What happens after "3.9% APR Financing for 36 Months"?
- Unable to compile upgrade parachain node after running `psvm v1.13.0`
- What to do about chain rubbing on unusually placed chainstay?
- Is it safer to sarcastically say "This is not a scam" than honestly say "This is a scam"?
- Type vs. Set Theory: Expressive Ability
- Which Jesus died or in what sense did Jesus ("God") die for our sins
- How do manganese nodules in the ocean sustain oxygen production without depleting over geological time scales?
- How do I prepare a longer campaign for mixed-experience players?
- but Ireland has ever been a shipwreck coast
- Is the writer or artist liable for a copyright violation?
- Using Gamma Ray Lasers to Blow Away Interstellar Medium
- How can life which cannot live on the surface of a planet naturally reach the supermajority of the planet's caves?
- What's the purpose of philosophy/knowledge?
- 7x10 floor and a 8x8 and a 6x1 carpet, only one cut allowed
- How to make sure a payment is actually received and will not bounce, using Paypal, Stripe or online banking respectively?
- Alignment of the currency symbol in a tabular environment
- Does anyone know if Edit Mode's "Make Line" Tool was deleted or moved in Blender 4.2?
- Estimate of a trigonometric product
- SDD drive unknown dropped from 500GB to 2GB capacity
- When I attach a sensitive pdf encrypted by Adobe with a password, and send it through Gmail with password included, does it make any difference?
- Isn't manual port forwarding actually less safe than UPnP?