Search code examples
varnishvarnish-vcl

selecting a varnish backend using regex capture


I would like to route subdomains in varnish using regex capture. Here is my attempt:

backend gitlab {
    .host = "127.0.0.1";
    .port = "82";
}

backend jenkins {
    .host = "127.0.0.1";
    .port = "83";
}

sub vcl_recv {

    if (req.http.host ~ "^((gitlab|jenkins|ruby))\.") {

        set req.backend = $1;
        return(pass);

    }

    error 405 "No service.";

}

How can this kind of construct be achieved in VCL? I'd rather not use a less elegant "if-then" pattern.


Solution

  • You can not refer to a backend with a string. The VCL compiler doesn't support it. You might be able to write a special director (a vmod) that does the work for you, but this would require you to bring out your copy of K&R and start digging around in the Varnish source tree.

    It would not surprise me if somebody will write a VMOD to do something like this some day. It will be very useful.