Skip to content

Instantly share code, notes, and snippets.

@davidstrauss
Created June 15, 2017 21:58
Show Gist options
  • Save davidstrauss/0bf8ff0ce8e8f138898a212762aac352 to your computer and use it in GitHub Desktop.
Save davidstrauss/0bf8ff0ce8e8f138898a212762aac352 to your computer and use it in GitHub Desktop.
sub vcl_recv {
#FASTLY recv
set req.http.ISO-3166-1 = client.geo.country_code;
set req.http.Path = req.url.path;
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);
}
// Use HTTP Status 486 to "86" the client based on Vary conditions.
if (beresp.http.Block) {
set beresp.status = 403;
set beresp.http.Vary = beresp.http.Block;
} else {
// Otherwise, vary on the Path.
if (beresp.http.Vary) {
set beresp.http.Vary = beresp.http.Vary ", Path";
} else {
set beresp.http.Vary = "Path";
}
}
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 = 0s;
}
return(deliver);
}
sub vcl_hash {
set req.hash += req.http.host;
// Do not assume variation based on URL, which will usually be added in vcl_fetch.
#FASTLY hash
return(hash);
}
sub vcl_hit {
#FASTLY hit
if (!obj.cacheable) {
return(pass);
}
return(deliver);
}
sub vcl_miss {
#FASTLY miss
return(fetch);
}
sub vcl_deliver {
#FASTLY deliver
return(deliver);
}
sub vcl_error {
#FASTLY error
}
sub vcl_pass {
#FASTLY pass
}
sub vcl_log {
#FASTLY log
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment