Skip to content

Instantly share code, notes, and snippets.

@timkelty
Created June 25, 2014 12:36
Show Gist options
  • Save timkelty/35625a90a6819c8f7927 to your computer and use it in GitHub Desktop.
Save timkelty/35625a90a6819c8f7927 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_miss {
# Don't cache purge request
if (req.request == "EE_PURGE") {
if (req.http.X-Purge-Url) {
#ban(req.url ~ req.http.X-Purge-Url);
purge;
error 200 "Purged";
} else {
ban("req.url ~ ^/.*$");
error 200 "Purged";
}
}
}
sub vcl_hit {
# Don't cache purge request (same as miss)
if (req.request == "EE_PURGE") {
if (req.http.X-Purge-Url) {
purge;
error 200 "Purged";
} else {
ban("req.url ~ ^/.*$");
error 200 "Purged";
}
}
}
sub vcl_recv {
# Need to retain original IP for logs
remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;
# Accept-Encoding and User Agent Vary.
# These need normalization to avoid polluting the cache
# with dupes.
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
remove req.http.Accept-Encoding;
}
}
if (req.http.user-agent ~ "MSIE") {
set req.http.user-agent = "MSIE";
} else {
set req.http.user-agent = "Mozilla";
}
# 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