Hi,

On Fri, Jul 10, 2015 at 04:32:38PM -0400, Phillip Decker wrote:
> Hello again,
> 
> I was migrating a setup from the older style acl-based host to backend
> mapping, to the newer map-based approach, e.g.
> 
> use_backend  %[req.hdr(host),lower,map_beg(mapping.conf,default)]
> 
> and that works fine.  But now I'm tempted to simply name the backends after
> each servername, (eg. 'mail' from mail.google.com, or 'maps' from
> maps.google.com).
> 
> Which in my imagination would look something like:
> 
> use_backend %[req.hdr_beg(host),lower]
> 
> But is there a way to substring or regex off the front of the host string,
> and pass that to the use_backend call?
> 
> In other words, continuing the google.com example, if people are hitting
> the server with "host: mail.google.com", and maps.google.com, and just for
> completeness, notes.books.google.com, I'd like to have backends:
> 
> backend mail
>    mode http
>    foo
>    ....
> 
> backend maps
>    mode http
>    bar
>    ...
> 
>  backend notes.books
>    mode http
>    baz
>    ..
> 
> Is there a way to accomplish this without building them into a mapped file
> in 1.5.x?
> 
> (1.6 solutions are okay too, but won't be able to move to that until it's
> in stable.)

In 1.6 there's the "word" converter which stops at the first delimiter,
so that if you use "word(1,.)" on "notes.books.domain.com", it will return
"notes". From what I understand, in your specific case it does not perfectly
respond to your needs but for many other cases that could be fine. Also it
makes sense to use it to cut the hostname only and ignore the port.

Otherwise there's the "regsub" converter, to which you pass a regex, a
substitute string, and optionally some flags. There are some limitations
such as the fact that the comma character cannot be used there due to a
limitation in the config language which uses it as an argument delimiter,
but for a domain it's not a problem.

Thus a fairly complete solution to your need above could look like this :

    use_backend %[req.hdr(host),word(1,:),lower,regsub(\.domain\.com$,)]

By replacing ".domain.com" at the end of the name with an empty string,
it will then return "maps", "mail", "notes.book" in your example above.

Regards,
Willy


Reply via email to