Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Advanced VCL: how to use restart

2,817 views

Published on

Paolo Alvarado Customer Support Engineer, Fastly at Altitude 2016
Customer Support Engineer Paolo Alvarado discusses various useful features of advanced Varnish Configuration Language (VCL).

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Advanced VCL: how to use restart

  1. 1. Main parts of Fastly request vcl_recv vcl_hash vcl_delivervcl_fetch End user
  2. 2. What is a restart? vcl_recv vcl_hash vcl_delivervcl_fetch restart
  3. 3. Why restart?
  4. 4. Why restart? •Checking Primary/Secondary Backends •Following Redirects
  5. 5. Checking a secondary origin based on a 
 404 response from the primary origin Today’s lessons Lesson 1 – Checking Primary/Secondary Origins
  6. 6. Checking primary/secondary origins
  7. 7. “File not found” restart in fetch vcl_recv vcl_hash vcl_fetch
  8. 8. “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
  9. 9. Catch the restart vcl_recv vcl_hash vcl_fetch restart
  10. 10. 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
  11. 11. Checking primary/secondary origins
  12. 12. Checking primary/secondary origins
  13. 13. Why restart? •Checking Primary/Secondary Backends •Following Redirects
  14. 14. 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
  15. 15. Following redirects
  16. 16. Creating new headers in deliver vcl_recv vcl_hash vcl_delivervcl_fetch
  17. 17. 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
  18. 18. Use regex set req.url = regsub(resp.http.Location,"^https?:// [^/]+(.*)","1"); ^ pulls out /path/* from www.example.com/path/*
  19. 19. 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; } …}
  20. 20. Catch the restart vcl_recv vcl_hash vcl_delivervcl_fetch restart
  21. 21. 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); } …}
  22. 22. Fix the hash vcl_recv vcl_hash
  23. 23. 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); } …}
  24. 24. Following redirects
  25. 25. 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
  26. 26. A word of caution
  27. 27. 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
  28. 28. Questions

×