Last active
February 25, 2022 17:11
-
-
Save jwieringa/da49080d432008aeade03c3117a10e6d to your computer and use it in GitHub Desktop.
Fastly Service VCL Custom Template - February 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sub vcl_recv { | |
#FASTLY recv | |
# Normally, you should consider requests other than GET and HEAD to be uncacheable | |
# (to this we add the special FASTLYPURGE method) | |
if (req.method != "HEAD" && req.method != "GET" && req.method != "FASTLYPURGE") { | |
return(pass); | |
} | |
return(lookup); | |
} | |
sub vcl_fetch { | |
#FASTLY fetch | |
# In the event of a server-failure response from origin, retry once more | |
if ((beresp.status == 500 || beresp.status == 503) && req.restarts < 1 && (req.method == "GET" || req.method == "HEAD")) { | |
restart; | |
} | |
# Log the number of restarts for debugging purposes | |
if(req.restarts > 0 ) { | |
set beresp.http.Fastly-Restarts = req.restarts; | |
} | |
# If the response is setting a cookie, make sure it is not cached | |
if (beresp.http.Set-Cookie) { | |
return(pass); | |
} | |
# By default we set a TTL based on the `Cache-Control` header | |
# but we don't parse additional directives like `private` and | |
# `no-store`. Private in particular should be respected at the edge: | |
if (beresp.http.Cache-Control ~ "(private|no-store)") { | |
return(pass); | |
} | |
# If the backend response is 500 or 503, then override | |
# Surrogate-Control, Cache-Control, and default TTL settings. | |
if (beresp.status == 500 || beresp.status == 503) { | |
set beresp.ttl = 1s; | |
set beresp.stale_if_error = 5s; | |
return(deliver); | |
} | |
# If no TTL has been provided in the response headers, set a default | |
if (!beresp.http.Expires && !beresp.http.Surrogate-Control ~ "max-age" && !beresp.http.Cache-Control ~ "(s-maxage|max-age)") { | |
set beresp.ttl = 3600s; | |
} | |
return(deliver); | |
} | |
sub vcl_hit { | |
#FASTLY hit | |
return(deliver); | |
} | |
sub vcl_miss { | |
#FASTLY miss | |
return(fetch); | |
} | |
sub vcl_deliver { | |
#FASTLY deliver | |
return(deliver); | |
} | |
sub vcl_error { | |
#FASTLY error | |
return(deliver); | |
} | |
sub vcl_pass { | |
#FASTLY pass | |
return(pass); | |
} | |
sub vcl_log { | |
#FASTLY log | |
} | |
sub vcl_hash { | |
set req.hash += req.url; | |
set req.hash += req.http.host; | |
#FASTLY hash | |
return(hash); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment