Varnish 3 – Exclude a virtual host

If you have multiple virtual hosts on your webserver, installing Varnish will cause all virtual hosts to be cached. With VCL it’s possible to exclude some virtual hosts.

sudo nano /etc/varnish/default.vcl
sub vcl_recv {
    # Don't cache domain1.com or domain2.org - optional www
    if (req.http.host ~ "(www\.)?(domain1.com|domain2.org)\.(com|nl|org|dev|local)") {
        return (pass);
    }
}

sub vcl_deliver {
    # You can optionally set a reponse header so it's easier for you to debug if Varnish really didn't cache objects for the host

    # Don't cache domain1.com or domain2.org - optional www
    if (req.http.host ~ "(www\.)?(domain1.com|domain2.org)\.(com|nl|org|dev|local)") {
        set resp.http.X-Cache = "EXCLUDED";
    }
}

After editing, execute:

sudo service varnish restart