15

I have been Googling aggressively, but without luck.

I'm using Varnish with great results, but I would like to host multiple websites on a single server (Apache), without Varnish caching all of them.

Can I specify what websites by URL to cache?

Thanks

Rune
  • 171
  • 1
  • 1
  • 8

3 Answers3

32

(edited after comment) It's req.http.host, so in your vcl file (e.g. default.vcl) do:

sub vcl_recv {
  # dont cache foo.com or bar.com - optional www
   if (req.http.host ~ "(www\.)?(foo|bar)\.com") {
     pass;
   }
  # cache foobar.com - optional www
   if (req.http.host ~ "(www\.)?foobar\.com") {
     lookup;
   }
}

And in varnish3-vcl:

sub vcl_recv {
  # dont cache foo.com or bar.com - optional www
   if (req.http.host ~ "(www\.)?(foo|bar)\.com") {
     return(pass);
   }
  # cache foobar.com - optional www
   if (req.http.host ~ "(www\.)?foobar\.com") {
     return(lookup);
   }
}
ivy
  • 5,539
  • 1
  • 34
  • 48
  • Great, pointed me in the right direction I think. But isn't it req.http.host? http://www.varnish-cache.org/trac/wiki/VCL – Rune Sep 17 '10 at 11:59
  • You're right, thanks! It's req.http.host (and req.url). I fixed it in the example. – ivy Sep 20 '10 at 15:37
  • Do you *have* to *pass* the foo and bar domains explicitly, or would it be enough to only *lookup* the desired domain? In other words, is it enough to define a whitelist? – Martijn Heemels May 03 '11 at 18:33
  • If you only define a white or a black-list (i.e.; you don't exit your own vcl_recv function with an action like pass, lookup, etc), then the default behavior of vcl_recv is applied. Please read about it at http://www.varnish-cache.org/trac/wiki/VCLExampleDefault and study default.vcl. To answer your question: yes you have to pass them. If you don't pass the foo and bar domains explicitly, and your GET or HEAD request contains no auth or cookie header, it will be looked up. – ivy Jun 13 '11 at 09:49
  • That works for Varnish 2. You should update your answer to include the Varnish 3 code too, which is return (pass) and return(pipe). – Mark Theunissen Oct 16 '12 at 22:32
  • If this doesn't work, make sure that `sub vcl_fetch {` has a corresponding `return(hit_for_pass);` wherever there's a `return(pass);` in `sub vcl_recv {` – yitwail May 04 '13 at 18:33
  • I would strongly recommend not hardcoding "return(lookup)". Clean up the request and then let the default policy decide if it can cache it or not. When forcing caching you're halfway to caching session cookies which can be disastrous. – perbu Mar 17 '14 at 11:34
6

Yes,

in vcl_recv you just match the hosts that you would like not to cache and pass them. Something like this (untested):

vcl_recv {
   # dont cache foo.com or bar.com - optional www
   if (req.host ~ "(www)?(foo|bar).com") {
     return(pass);
   }
}
perbu
  • 390
  • 1
  • 7
2

For Varnish 4

replace lookup with hash

default.vcl:

sub vcl_recv {
  # dont cache foo.com or bar.com - optional www
   if (req.http.host ~ "(www\.)?(foo|bar)\.com") {
     return(pass);
   }
  # cache foobar.com - optional www
   if (req.http.host ~ "(www\.)?foobar\.com") {
     return(hash);
   }
}
Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65