Skip to content

Instantly share code, notes, and snippets.

@leonsodhi
Created June 12, 2015 00:54
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 leonsodhi/75749490b86e2d50223c to your computer and use it in GitHub Desktop.
Save leonsodhi/75749490b86e2d50223c to your computer and use it in GitHub Desktop.
# Called at the beginning of a request, after the complete request has been received and parsed
sub vcl_recv {
#FASTLY recv
if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") {
return(pass);
}
return(lookup);
}
# Used to alter the response headers, trigger ESI processing, or try alternate backend servers in case the request failed
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;
}
return(deliver);
}
# Called after an object has been found (hit) in the cache
sub vcl_hit {
#FASTLY hit
if (!obj.cacheable) {
return(pass);
}
return(deliver);
}
# Called after an object was not found in the cache
sub vcl_miss {
#FASTLY miss
return(fetch);
}
# Used to add and/or remove headers
sub vcl_deliver {
#FASTLY deliver
unset resp.http.X-Log-Message;
}
# Generates content from within Varnish, without talking to a web server
sub vcl_error {
#FASTLY error
}
# Called after a pass in vcl_recv, or after a lookup that returned a hit pass
sub vcl_pass {
#FASTLY pass
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment