Created
February 10, 2016 15:38
-
-
Save mshmsh5000/2a5fb35db6df605e311b to your computer and use it in GitHub Desktop.
Fastly VCL for redirects stored in dicts
This file contains 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 { | |
if (table.lookup(redirects, req.url)) { | |
error 777 "Moved"; | |
} | |
#FASTLY recv | |
if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") { | |
return(pass); | |
} | |
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; | |
} | |
return(deliver); | |
} | |
sub vcl_hit { | |
#FASTLY hit | |
if (!obj.cacheable) { | |
return(pass); | |
} | |
return(deliver); | |
} | |
sub vcl_miss { | |
#FASTLY miss | |
return(fetch); | |
} | |
sub vcl_deliver { | |
set resp.http.X-Fastly-Original = req.url; | |
set resp.http.X-Fastly-Lookup = table.lookup(redirects, req.url, "None"); | |
#FASTLY deliver | |
return(deliver); | |
} | |
sub vcl_error { | |
# Could be a 301 or 302 -- need to determine. Default to 302. | |
if (obj.status == 777) { | |
set obj.http.Location = table.lookup(redirects, req.url); | |
set obj.status = std.atoi(table.lookup(redirect_types, req.url, "302")); | |
return(deliver); | |
} | |
#FASTLY error | |
} | |
sub vcl_pass { | |
#FASTLY pass | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment