Skip to content

Instantly share code, notes, and snippets.

@lewisjenkins
Forked from timkelty/default.vcl
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lewisjenkins/d9d0cb0a95eb0a1b837f to your computer and use it in GitHub Desktop.
Save lewisjenkins/d9d0cb0a95eb0a1b837f to your computer and use it in GitHub Desktop.
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
# Switch to 8080 when we go live.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Site specific
if ( !(req.url ~ "^/favicon.ico" ||
req.url ~ "^/scripts/" ||
req.url ~ "^/assets/" ||
req.url ~ "^/cache" ||
req.url ~ "^/files/" ||
req.url ~ "^/docs/.*" ||
req.url ~ "^/cp/.*" ||
req.http.Cookie ~ ".*exp_sessionid.*" ||
req.request == "POST" ||
req.http.X-Requested-With == "XMLHttpRequest" ) ){
remove req.http.Cookie;
remove req.http.Cache-Control;
remove req.http.Max-Age;
remove req.http.Pragma;
set req.http.cacheme = "1";
} else {
return (pass);
}
return (lookup);
}
sub vcl_fetch {
if (beresp.http.Cache-Control ~ "no-cache") {
return (hit_for_pass);
}
if (beresp.status == 403) {
set beresp.ttl = 0s;
return (hit_for_pass);
}
if (beresp.status >= 500) {
set beresp.ttl = 0s;
return (hit_for_pass);
}
/* Remove expires headers and use cache-control for non-assets. */
if ( !(beresp.http.Content-Type ~ ".*(javascript|css|image).*" )) {
unset beresp.http.expires;
set beresp.http.cache-control = "max-age=900";
}
if (req.http.cacheme ~ "1") {
unset beresp.http.Cookie;
unset beresp.http.Cache-Control;
unset beresp.http.Max-Age;
unset beresp.http.Pragma;
set beresp.ttl = 1w;
}
/* marker for vcl_deliver to reset Age: */
set beresp.http.magicmarker = "1";
# Varnish determined the object was not cacheable
if (beresp.ttl <= 0s) {
set beresp.http.X-Cacheable = "NO:Not Cacheable";
# You don't wish to cache content for logged in users
} elsif (req.http.Cookie ~ "exp_sessionid") {
set beresp.http.X-Cacheable = "NO:Got Session";
return(hit_for_pass);
# You are respecting the Cache-Control=private header from the backend
} elsif (beresp.http.Cache-Control ~ "private") {
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
return(hit_for_pass);
# Varnish determined the object was cacheable
} else {
set beresp.http.X-Cacheable = "YES";
}
return(deliver);
}
sub vcl_deliver {
# This is for debugging cache misses
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
/* Set in vcl_fetch. for expires headers */
if (resp.http.magicmarker) {
/* Remove the magic marker */
unset resp.http.magicmarker;
/* By definition we have a fresh object */
set resp.http.age = "0";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment