Main parts of Fastly request
vcl_recv vcl_hash vcl_delivervcl_fetch End user
What is a restart?
vcl_recv vcl_hash vcl_delivervcl_fetch
restart
Why restart?
Why restart?
•Checking Primary/Secondary Backends
•Following Redirects
Checking a secondary origin based on a 

404 response from the primary origin
Today’s lessons
Lesson 1 – Checking Primary/Secondary Origins
Checking primary/secondary origins
“File not found” restart in fetch
vcl_recv vcl_hash vcl_fetch
“File not found” restart in fetch
vcl_fetch {
if (beresp.status == 404 && req.restarts == 0) {
restart;
}
#FASTLY FETCH

…}
^ if the origin response = 404 then restart.
Restart – resets the vcl state machine (back to recv)
vcl_recv
vcl_hash
vcl_fetch
Catch the restart
vcl_recv vcl_hash vcl_fetch
restart
Catch the restart
vcl_recv
vcl_hash
vcl_fetch
vcl_recv {
if (req.restarts > 0) {
set req.backend = F_secondary;
set req.http.Fastly-Force-Shield = "1";
return(lookup);
}
#FASTLY RECV
…}
^ if we restarted, switch backends, re-enable clustering, and
lookup the content
Checking primary/secondary origins
Checking primary/secondary origins
Why restart?
•Checking Primary/Secondary Backends
•Following Redirects
Following a 301 redirect and rewriting the request so
we can remove the 301 from the cached result.
Today’s lessons
Lesson 2 – Following redirects
Following redirects
Creating new headers in deliver
vcl_recv vcl_hash vcl_delivervcl_fetch
Creating new headers in deliver
set req.http.OrigHost = req.http.host;
set req.http.OrigURL = req.url;
^ stores the initial request’s host and url into headers
that we can use later
vcl_recv
vcl_hash
vcl_deliver
vcl_fetch
Use regex
set req.url =
regsub(resp.http.Location,"^https?://
[^/]+(.*)","1");
^ pulls out /path/* from www.example.com/path/*
Final result
vcl_recv
vcl_hash
vcl_deliver
vcl_fetch
vcl_deliver {
#FASTLY deliver
if (resp.status == 301 && req.restarts == 0) {
set req.http.OrigHost = req.http.host;
set req.http.OrigURL = req.url;
set req.url =
regsub(resp.http.Location,"^https?://[^/]+
(.*)","1");
restart;
}
…}
Catch the restart
vcl_recv vcl_hash vcl_delivervcl_fetch
restart
Catch the restart
vcl_recv
vcl_hash
vcl_deliver
vcl_fetch
vcl_recv
if (req.http.OrigHost) {
set req.http.Fastly-Force-Shield = "1";
set req.backend = F_redirectBackend;
set req.http.host = "www.example.com";
return(lookup);
}
…}
Fix the hash
vcl_recv vcl_hash
Fix the hash
vcl_hash {
if(req.http.OrigHost) {
set req.hash += req.http.OrigURL;
set req.hash += req.http.OrigHost;
set req.hash += "#####GENERATION#####";
return (hash);
}
…}
Following redirects
Summary
1.Restart in fetch or deliver (if you alter headers)
2.Catch the restart in recv
3.Optional: change the hash
4.Enjoy VCL
A word of caution
Learning VCL
We have plenty to offer:
Check out our Docs page: https://docs.fastly.com/guides/vcl/
Send questions to support@fastly.com
#fastly in irc.freenode.net
Questions

Advanced VCL: how to use restart