HAProxy community

Map wildcard domain to backend via map


In my current setup I have a backends.map file mapping fqdn to backend name:

backends.map file:
somehost.somedomain.com back-someservice

Frontend use_backend line:
use_backend %[req.hdr(host),lower,map_dom(/etc/haproxy/backends.map)]

Now I’d like to map a wildcard domain to a backend such that:

hosta.somedomain.com -> back-servicea
hostb.somedomain.com -> back-servicea
hostc.somedomain.com -> back-servicea

I don’t want to specify each line individually, I’d rather use a regex or wildcard to do this. For example:

backends.map file:
*.somedomain.com back-servicea

Tried doing this with the way I have the current use_backend line setup and that does not seem to work. Anyone know how this can be accomplished?


You should be able to do this with map_reg (regex) or any of the other map_ methods, like map_end or map_sub, whatever works best for you:

Iam trying to get something similar to work, I would also like to map wildcard

frontend somewildcard_sub.mydomain.com to somewildcard_sub_myservice backend
without repeating all those frontends to backends mappings.

If I were to use map_reg then I’d have to have all regexes in the backends.map file, and I don’t want to do that. I ended up creating an acl, then putting a use_backend entry if before the use_backend on the map, like this:

acl is_statsurl hdr(host) -m end stats.domain.com
use_backend back-stats if is_statsurl
use_backend %[req.hdr(host),lower,map_dom(/etc/haproxy/backends.map)]

that way all *.stats.domain.com requests are routed to the stats backend, while others route via the map.