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