Setup Varnish - Multiple Domains

From Brian Nelson Ramblings
Jump to: navigation, search

Setup Varnish for Multiple Domains

Many times you will have on instance of Varnish on your server but you will have multiple domains on your server. You will need to create separate backends for each domain.

Below is an example of using two separate domains, with different internal ip.

backend example1 {
    .host = "127.0.0.1";
    .port = "81";
}
backend example2 {
     .host = "127.0.0.2";
     .port = "81";
}
sub vcl_recv {
   if (req.http.host == "example1.com") {
       #You will need the following line only if your backend has multiple virtual host names
       set req.http.host = "example1.com";
       set req.backend = example1;
       return (lookup);
   }
   if (req.http.host == "example2.com") {
       #You will need the following line only if your backend has multiple virtual host names
       set req.http.host = "example2.com";
       set req.backend = example2;
       return (lookup);
   }
}

If you need to setup one of your domains to listen for both www and non-www

if (req.http.host ~ "^(www\.)?briansnelson\.com$") {
       set req.http.host = "briansnelson.com";
       set req.backend = example2;
       return (lookup);
}

If you need the setup to be used for any sub-domain

if (req.http.host ~ "^(.*\.)?briansnelson\.com$") {
       set req.http.host = "briansnelson.com";
       set req.backend = example2;
       return (lookup);
}

Additional Articles