Advanced VCL: how to use restart

28
Main parts of Fastly request vcl_recv vcl_hash vcl_deliver vcl_fetch End user

Transcript of Advanced VCL: how to use restart

Page 1: Advanced VCL: how to use restart

Main parts of Fastly request

vcl_recv vcl_hash vcl_delivervcl_fetch End user

Page 2: Advanced VCL: how to use restart

What is a restart?

vcl_recv vcl_hash vcl_delivervcl_fetch

restart

Page 3: Advanced VCL: how to use restart

Why restart?

Page 4: Advanced VCL: how to use restart

Why restart?

•Checking Primary/Secondary Backends•Following Redirects

Page 5: Advanced VCL: how to use restart

Checking a secondary origin based on a 404 response from the primary origin

Today’s lessons

Lesson 1 – Checking Primary/Secondary Origins

Page 6: Advanced VCL: how to use restart

Checking primary/secondary origins

Page 7: Advanced VCL: how to use restart

“File not found” restart in fetch

vcl_recv vcl_hash vcl_fetch

Page 8: Advanced VCL: how to use restart

“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

Page 9: Advanced VCL: how to use restart

Catch the restart

vcl_recv vcl_hash vcl_fetch

restart

Page 10: Advanced VCL: how to use 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

Page 11: Advanced VCL: how to use restart

Checking primary/secondary origins

Page 12: Advanced VCL: how to use restart

Checking primary/secondary origins

Page 13: Advanced VCL: how to use restart

Why restart?

•Checking Primary/Secondary Backends•Following Redirects

Page 14: Advanced VCL: how to use restart

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

Page 15: Advanced VCL: how to use restart

Following redirects

Page 16: Advanced VCL: how to use restart

Creating new headers in deliver

vcl_recv vcl_hash vcl_delivervcl_fetch

Page 17: Advanced VCL: how to use restart

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

Page 18: Advanced VCL: how to use restart

Use regex

set req.url = regsub(resp.http.Location,"^https?://[^/]+(.*)","\1");

^ pulls out /path/* from www.example.com/path/*

Page 19: Advanced VCL: how to use restart

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; } …}

Page 20: Advanced VCL: how to use restart

Catch the restart

vcl_recv vcl_hash vcl_delivervcl_fetch

restart

Page 21: Advanced VCL: how to use 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);

} …}

Page 22: Advanced VCL: how to use restart

Fix the hash

vcl_recv vcl_hash

Page 23: Advanced VCL: how to use restart

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);

} …}

Page 24: Advanced VCL: how to use restart

Following redirects

Page 25: Advanced VCL: how to use restart

Summary

1.Restart in fetch or deliver (if you alter headers)2.Catch the restart in recv3.Optional: change the hash4.Enjoy VCL

Page 26: Advanced VCL: how to use restart

A word of caution

Page 27: Advanced VCL: how to use restart

Learning VCL

We have plenty to offer:Check out our Docs page: https://docs.fastly.com/guides/vcl/Send questions to [email protected]#fastly in irc.freenode.net

Page 28: Advanced VCL: how to use restart

Questions