Skip to content

Instantly share code, notes, and snippets.

@forficate
Created July 2, 2014 14:03
Show Gist options
  • Save forficate/7cdaee39cf6712f6893a to your computer and use it in GitHub Desktop.
Save forficate/7cdaee39cf6712f6893a to your computer and use it in GitHub Desktop.
sub vcl_recv {
#FASTLY recv
if (req.request != "HEAD" && req.request != "GET" && req.request != "PURGE") {
return(pass);
}
#Dissable access to these at varnish
if (req.url ~ "install\.php|update\.php|cron\.php|apc.php|user/register") {
error 404 "Page not found.";
}
#Strip cookies on assets
if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js|woff)(\?.*)?$") {
unset req.http.Cookie;
}
if (req.http.Cookie) {
set req.http.Cookie = ";" req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|SSESS[a-z0-9]+|NO_CACHE)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.Cookie == "") {
# If there are no remaining cookies, remove the cookie header. If there
# aren't any cookie headers, Varnish's default behavior will be to cache
# the page.
unset req.http.Cookie;
}
else {
# If there is any cookies left (a session or NO_CACHE cookie), do not
# cache the page.
return (pass);
}
}
#Do node/add check after the cookie check, as if logged in we won't get here due to the pass
if(req.url ~ "node/add") {
error 404 "Page not found.";
}
// Normalize the Accept-Encoding header
// as per: http://varnish-cache.org/wiki/FAQ/Compression
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|woff)$") {
# No point in compressing these
remove req.http.Accept-Encoding;
}
elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
}
else {
# Unknown or deflate algorithm
remove req.http.Accept-Encoding;
}
}
return(lookup);
}
sub vcl_fetch {
#FASTLY fetch
if ((beresp.status == 500 || beresp.status == 503) && req.restarts < 1 && (req.request == "GET" || req.request == "HEAD")) {
restart;
}
if(req.restarts > 0 ) {
set beresp.http.Fastly-Restarts = req.restarts;
}
if (beresp.http.Set-Cookie) {
set req.http.Fastly-Cachetype = "SETCOOKIE";
return (pass);
}
if (beresp.http.Cache-Control ~ "private") {
set req.http.Fastly-Cachetype = "PRIVATE";
return (pass);
}
if (beresp.status == 500 || beresp.status == 503) {
set req.http.Fastly-Cachetype = "ERROR";
set beresp.ttl = 1s;
set beresp.grace = 5s;
return (deliver);
}
if (beresp.http.Expires || beresp.http.Surrogate-Control ~ "max-age" || beresp.http.Cache-Control ~"(s-maxage|max-age)") {
# keep the ttl here
} else {
# apply the default ttl
set beresp.ttl = 3600s;
}
//If static asset
if (req.url ~ "(?i)\.(pdf|asc|dat|txt|doc|xls|ppt|tgz|csv|png|gif|jpeg|jpg|ico|swf|css|js|woff)(\?.*)?$") {
unset beresp.http.set-cookie;
} else {
if(!beresp.http.Surrogate-Control) {
set beresp.http.Surrogate-Control = "max-age=3600"; #Force FASTLY to cache internally for 1 Hour
}
remove beresp.http.Cache-Control;
set beresp.http.Cache-Control = " private, max-age=0, no-cache"; #No public cache so we can use purge
}
return(deliver);
}
sub vcl_hit {
#FASTLY hit
if (!obj.cacheable) {
return(pass);
}
return(deliver);
}
sub vcl_miss {
#FASTLY miss
return(fetch);
}
sub vcl_deliver {
#FASTLY deliver
remove resp.http.X-Powered-By; #Let's not give away to much server info
remove resp.http.X-Generator; #Let's not give away to much server info
remove resp.http.X-Drupal-Cache; #Always going to be a miss so don't need this
return(deliver);
}
sub vcl_error {
#FASTLY error
}
sub vcl_pass {
#FASTLY pass
}
sub vcl_hash {
if (req.http.Cookie) {
set req.hash += req.http.Cookie;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment