List of hosts in Varnish

cachevarnish

I'm running a VPS with multiple different websites, and Varnish in front for caching.
However, some websites should not be cached.
Instead of making individual rules for each website, I would like to make a general "DON'T CACHE" list, which is linked to some rules. Possible?

In the following, I've sketched what I would like: a list of websites (ACL syntax) in the VCL configuration which should not be cached.

list cache_blacklist {
 "domain1.com";
 "domain2.com";
}

sub vcl_recv {
  if (req.http.host ~ cache_blacklist) {
    return(pass);
  }   
}

sub vcl_fetch {
  if (req.http.host ~ cache_blacklist) {
    return(hit_for_pass);
  }
}

Best Answer

First thing : you don't need to set a vcl_fetch rule if the condition depends on the request. Everything will be handled in vcl_recv.

vcl_fetch rules are only needed when condition depends on the server response.

HTTP Host being in the request...your vcl_fetch rule is actually useless.

Now that you don't need to tell "which domains should not be cached" twice, just use a single condition in vcl_recv like this :

sub vcl_recv {
    if (req.http.host == "domain1.com" || 
        req.http.host == "domain2.com") {
        return(pass);
    }
}

Note that you can also use regexp...not sure about which is the best...