HAProxy community

Condition with path_reg and map_dom with regexps



I’d like to match a sample (path) with a regular expression read from a map (with hdr(host) as a key). Is this possible somehow?

Example: Given a request with hdr(host) set to ‘foo.com’ and path set to /bar I’d like to have a map file like this:
foo.com ^/bar/.*

I posted basically the same question on stackoverflow before I found this community.

Thanks in advance


base contains a concatenation of hostname and path, that is what you want to match your map file against, it’s impossible to do so with 2 different variables (hostname AND path).

Then you can use string matches, substring matches or even regex - whatever you want, but against a single string.

$ cat /etc/haproxy/base_to_backend.map
foo.com/bar/ foo

Exact string match:

use_backend bk-%[base,lower,map_str(/etc/haproxy/base_to_backend.map,bk_default)]

“beg” string match:

use_backend bk-%[base,lower,map_beg(/etc/haproxy/base_to_backend.map,bk_default)]

Or differently, you really need a regexp:

$ cat /etc/haproxy/base_to_backend.map
foo.com/(bar/|bob/|alice/) foo

use_backend bk-%[base,lower,map_reg(/etc/haproxy/base_to_backend.map,bk_default)]

Configs are untested, but you get the point.