0

Is it possible to disable Varnish based on some condition set at the backend?

I have this use case:

Website powered by Varnish, has its administration area, where administrator has action for turning off page cache (Varnish). That will eventually set appropriate flag at some point, in some storage, depending on the implementation itself, etc.

The question is, can Varnish somehow query backend at some URL and then force return (pass) for that backend if result is negative, based on mentioned flag?

Thanks in advance.

Nikola Poša
  • 153
  • 1
  • 1
  • 13

1 Answers1

1

You can use Varnish probes [1] and req.backend.healthy to perform such behaviour, by example:

probe caching {
  .url = "/caching_on.txt";
  .interval = 1s;
  .timeout = 1s;
}
backend default {
  .host = "127.0.0.1";
  .port = "8080";
  .probe = basic;
}
backend alternative {
  .host = "127.0.0.1";
  .port = "8080";
}
sub vcl_recv {
  if (! req.backend.healthy) {
    set req.backend = alternative;
    return (pass);
  }
  #...
}
# ...

With that piece of code if HTTP response for /caching_on.txt is not 200 varnish will switch backend to alternative and pass the request.

[1] https://www.varnish-cache.org/docs/3.0/reference/vcl.html#backend-probes

NITEMAN
  • 1,236
  • 10
  • 11
  • I read somewhere that probe will eventually result in Varnish not sending any traffic to a backend, after it is resolved as "sick", which means that site will be down, if there is only one backend. Am I right about that? But also, I hope that this snippet in your example: if (! req.backend.healthy) { //etc } solves that, by forcing pass, instead of site failure? Is that true? – Nikola Poša Dec 29 '13 at 13:26
  • If you read carefully the snippet you'll see that there are 2 different backend definitions and if default backend is marked as sick the backend is set to alternative (which doesn't have any health probe). So give it a try... – NITEMAN Dec 29 '13 at 19:01
  • But that alternative backend is actually same as a default one (same host/port), right? Is in that a trick? – Nikola Poša Dec 29 '13 at 20:31
  • Call it a trick, for varnish a different backend declaration is a different backend no matter the host/port combination (and it's quite usual to use this... by example to apply different timeouts to different kind of requests) – NITEMAN Dec 29 '13 at 20:41