wiki:VCLExampleSimpleRestarts

Version 1 (modified by gregcoit, 4 years ago) (diff)

--

If apache or mysql goes down (or other misconfiguration happens), apache gives varnish a 5xx. Varnish knows better than to cache this page, but it will pass it through to the user if the object grace has expired. Fortunately, varnish has the ability to reset the grace and serve the "good" page from cache until apache returns a non-5xx on that page.

In your /etc/varnish/default.vcl:

sub vcl_recv {
     set req.grace = 30s;
}

sub vcl_fetch{
     if ( obj.status == 500) {
         set obj.grace = 60s;
         restart;
     }
{

This tells varnish to check the source object at most every 1 minutes to see of the status has changed. If apache still returns a 5xx (or doesn't respond), then the grace is reset and the user if given the cached page.

Note that this only works for anonymous users - authenticated users are usually excluded from seeing cached pages.