Is Varnish 4 able to follow a redirect and cache the destination?

Andreas Plesner apj at mutt.dk
Fri Dec 2 09:51:38 CET 2016


On Thu, Dec 01, 2016 at 05:23:19PM -0500, Star Dot wrote:

> Varnish is happily caching the 302 response only, but I'm curious if I can
> somehow follow the redirect and cache the destination file completely? This
> will alleviate load off the nginx server obviously.

Yes. This is what we do:

sub vcl_deliver {
  if ((resp.status == 301) || (resp.status == 302)) {
    set req.url = regsub(resp.http.Location,"^http://[^/]+(.*)","\1");
    return(restart);
  }
}

Handling of the host header (and all the possible backends) is left as an
exercise for the reader.

What this does is that it caches the redirect as well as the destination, thus
sort-of normalizing the request, while still caching the ultimate destination
only once.

-- 
Andreas



More information about the varnish-misc mailing list