Search code examples
apachevarnishvarnish-vcl

Using Varnish with multiple apache named vhosts


I'm implementing Varnish (4.0) for a server with lots (1000+) of named virtual hosts (on Apache), from which most of them point to the same IP- and web. I get Varnish to work fine with:

backend default {
  .host = "127.0.0.1";
  .port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^www.domain1.de(:[0-9]+)?$") {
    set req.http.host = "www.domain1.de";
 } else if (req.http.host ~ "^www.domain2.de(:[0-9]+)?$") {
    set req.http.host = "www.domain2.de";
 }
 ....
 ....
 set req.backend_hint = default;
}

but, to do this for 1000+ domains seems a bit odd. I don't need any special configuration for the sites, they have all the same backend.

If I don't add any specific configuration, I only get to the standard website (no matter what domain I enter).

Any hint on how to solve that? Thanks!


Solution

  • If you wish to remove the port name for example, or need to do some changes in general to the req.http.host you can use the regsub() method in your varnish VCL:

    set req.http.host = regsub(req.http.host , "(.*)(:[0-9]+|)" , "\1" );
    

    This example removes the port number if present.
    Please set up the regexp according to your needs as your question does not really state what you are trying to achieve.

    Note that you can invoke the replacement strings via \N and not as $1 as some man pages suggest. (A bug has already been filed to address this issue.)

    And for last a nice Varnish regexp cheat-sheet:
    http://kly.no/varnish/regex.txt