Skip to content

Instantly share code, notes, and snippets.

@jeremyjbowers
Created December 31, 2011 03:59
Show Gist options
  • Save jeremyjbowers/1542813 to your computer and use it in GitHub Desktop.
Save jeremyjbowers/1542813 to your computer and use it in GitHub Desktop.
The "miss" portion of a request in Varnish
sub vcl_recv {
# Set the backend for the request if it should miss the cache.
set req.backend = backend;
# Send the request along to the cache lookup.
return(lookup);
}
sub vcl_miss {
# You can have conditional logic here, but for now, let's just fetch from the backend.
return(fetch);
}
sub vcl_fetch {
# Fetching an object from the backend will also place it in the cache, usually.
# Set a 1 hour cache lifetime on this object that we're fetching from the backend.
set beresp.ttl = 1h;
# Set this backend request as cacheable.
set beresp.http.X-Cacheable = "YES";
# Turn off the Vary header.
# Some backends (*cough* Django *cough*) will set a header based on the User-Agent
# of the request. Varnish will cache a different version of the page for every
# single variation of User-Agent that visits your site. This is very bad. So we
# strip the Vary header off before delivering the request to the cache.
unset beresp.http.Vary;
# Deliver the object to the cache.
return(deliver);
}
sub vcl_deliver {
# Now, deliver the result to the browser.
return(deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment