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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top