HAProxy community

Prepend to path conditionally

I am trying to conditionally prepend a subdomain to the path.

Incoming https://<subdomain>.mysite.com/<path>

This should become https://<subdomain>.mysite.com/<subdomain>/<path>

but only if the path does not already start with . For example:

https://abc.mysite.com/jungle should become https://abc.mysite.com/abc/jungle

but https://abc.mysite.com/abc/jungle should not be changed.

I have this in haproxy.cfg. It correctly isolates the subdomain in var(req.store_id) but it prepends to the path enve if the store_id

    acl insti_prod         hdr_reg(host) -i [^.]*.mysite.com

    http-request set-var(req.store_id) req.hdr(host),lower,regsub(\.mysite\.com$,) if insti_prod

    # Pre-pend subdomain to the path if not already there
    http-request set-path /%[var(req.store_id)]%[path] if { var(req.store_id) -m found } !{ path_beg /var(req.store_id) }

Where am I going wrong??



path_beg /var(req.store_id) is not doing what you expect. It will try to match path against that string, not the variable content. When you write an ACL, the list of patterns you want to match against must be known at startup time. If you want to match dynamic values you have to use the strcmp converter for strings and sub for integers.

Try with the following:

  http-request set-var(req.foo) hdr(host),field(1,.)
  http-request set-path /%[var(req.foo)]%[path] unless { path,field(2,/),strcmp(req.foo) eq 0 }


This works beautifully! Thank you for the clear explanation.