Skip to content

Instantly share code, notes, and snippets.

@wpsmithtwc
Created May 9, 2017 14:53
Show Gist options
  • Save wpsmithtwc/fce1682c6b3c9dae114746297093f778 to your computer and use it in GitHub Desktop.
Save wpsmithtwc/fce1682c6b3c9dae114746297093f778 to your computer and use it in GitHub Desktop.
Fastly Default Main VCL
sub vcl_recv {
# Note: the convention of if (req.http.Fastly-FF) indicates this should only happen on the shield. Use !Fastly-FF to only execute on the edge
if (req.http.Fastly-FF) {
set req.max_stale_while_revalidate = 0s;
}
# Note: do not remove the macros below, this inserts whatever is configured in the web portal, and is required
#FASTLY recv
# Below is the default to not cache POSTs
if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") {
return(pass);
}
return(lookup);
}
sub vcl_fetch {
/* handle 5XX (or any other unwanted status code) */
if (beresp.status >= 500 && beresp.status < 600) {
/* deliver stale if the object is available */
if (stale.exists) {
return(deliver_stale);
}
if (req.restarts < 1 && (req.request == "GET" || req.request == "HEAD")) {
restart;
}
/* else go to vcl_error to deliver a synthetic */
error 503;
}
/* set stale_if_error and stale_while_revalidate (customize these values) */
set beresp.stale_if_error = 86400s;
set beresp.stale_while_revalidate = 60s;
#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.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 = 86400s;
}
# Set CORS headers
set beresp.http.access-control-allow-origin = "*";
set beresp.http.access-control-allow-methods = "GET,POST";
set beresp.http.access-control-allow-headers = "*";
set beresp.http.access-control-allow-credentials = "false";
set beresp.http.access-control-max-age = "86400";
# Custom cache rules
return(deliver);
}
sub vcl_hit {
#FASTLY hit
if (!obj.cacheable) {
return(pass);
}
return(deliver);
}
sub vcl_miss {
#FASTLY miss
return(fetch);
}
sub vcl_deliver {
unset resp.http.content-disposition;
if (resp.status >= 500 && resp.status < 600) {
/* restart if the stale object is available */
if (stale.exists) {
restart;
}
}
#FASTLY deliver
return(deliver);
}
sub vcl_error {
#FASTLY error
/* handle 503s */
if (obj.status >= 500 && obj.status < 600) {
/* deliver stale object if it is available */
if (stale.exists) {
return(deliver_stale);
}
/* otherwise, return a synthetic */
/* include your HTML response here */
# TODO -- insert a default image?
synthetic {"<!DOCTYPE html><html>An error has occured.</html>"};
return(deliver);
}
}
sub vcl_pass {
#FASTLY pass
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment