Slide 1

Slide 1 text

VARNISH FOR PHP DEVELOPERS

Slide 2

Slide 2 text

WHAT'S THIS TALK ABOUT?

Slide 3

Slide 3 text

HI, I'M MATTIAS GENIAR!

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

WHAT IS VARNISH?

Slide 6

Slide 6 text

IMPORTANT VARNISH VERSIONS

Slide 7

Slide 7 text

WHY USE VARNISH?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

REMINDER: HTTP HEADERS

Slide 10

Slide 10 text

REQUEST HEADERS

Slide 11

Slide 11 text

GET / HTTP/1.1 Accept: */* Accept-Encoding: gzip, deflate Host: ma.ttias.be User-Agent: Chrome/52 Cookie: laravel_session=eyJpdiI6...

Slide 12

Slide 12 text

RESPONSE HEADERS HTTP/1.1 200 OK Cache-Control: private, max-age=0 Content-Encoding: gzip Content-Length: 9944 Content-Type: text/html; charset=UTF-8 Server: Apache Date: Tue, 23 Aug 2016 14:15:50 GMT

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

HOW VARNISH WORKS

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

VARNISH CONFIGURATION LANGUAGE $ cat /etc/varnish/default.vcl vcl 4.0; sub vcl_recv { } sub vcl_backend_response { } sub vcl_backend_fetch { } sub vcl_deliver { }

Slide 29

Slide 29 text

MANIPULATING THE CLIENT REQUEST sub vcl_recv { # Only cache GET or HEAD requests. if (req.method != "GET" && req.method != "HEAD") { return (pass); } # Remove any Google Analytics based cookies set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", ""); set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", ""); ... }

Slide 30

Slide 30 text

MANIPULATING THE CLIENT REQUEST sub vcl_recv { if (req.http.host ~ "^(www\.)?domain\.tld$" || req.http.host ~ "test"){ set req.backend = specific_backend; return (pass); } }

Slide 31

Slide 31 text

MANIPULATING THE CLIENT REQUEST sub vcl_recv { if (req.url ~ "^[^?]*\.(css|gif|gz|ico|jpeg|jpg|js|png|xml)(\?.*)?$") { unset req.http.Cookie; return (lookup); } }

Slide 32

Slide 32 text

MANIPULATING THE RESPONSE sub vcl_backend_response { if (bereq.url ~ "^[^?]*\.(css|gif|gz|ico|jpeg|jpg|js|png|xml)(\?.*)?$") { unset bereq.http.Set-Cookie; # Case insensitive headers return (deliver); } }

Slide 33

Slide 33 text

MANIPULATING THE RESPONSE sub vcl_backend_response { # Don't cache 50x responses if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503) { return (abandon); } # Allow stale content, in case the backend goes down. # make Varnish keep all objects for 2 hours beyond their TTL set beresp.grace = 2h; }

Slide 34

Slide 34 text

MANIPULATING THE DELIVERY sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT"; } else { set resp.http.X-Cache = "MISS"; } set resp.http.X-Cache-Hits = obj.hits; unset resp.http.X-Powered-By; unset resp.http.Server; }

Slide 35

Slide 35 text

CHALLENGE: VISUALISING CHANGES

Slide 36

Slide 36 text

EXAMPLE +

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

DETERMINING HASH KEYS sub vcl_hash { hash_data(req.url); // ie: /blog/your-clickbait-title hash_data(req.http.host); // ie: ma.ttias.be hash_data(req.http.Cookie); // ie: language=nl }

Slide 44

Slide 44 text

CHALLENGE: URLS sub vcl_recv { # Sort all query arguments set req.url = std.querysort(req.url); // ie: "?lang=nl&p=5" == "?p=5&lang=nl" # Remove GA tags: domain.tld?utm_source=email&utm_medium=buffer&... set req.url = regsuball(req.url, "(utm_source|utm_medium|...)=(...)", ""); }

Slide 45

Slide 45 text

CHALLENGE: COOKIES sub vcl_hash { // ... hash_data(req.http.Cookie); // ie: language=nl }

Slide 46

Slide 46 text

CHALLENGE: COOKIES import cookie; sub vcl_recv { # Use libvmod-cookie, parse the "Cookie:" header from the client cookie.parse(req.http.cookie); # Filter all except these cookies from it cookie.filter_except("laravel_session,custom_cookie"); # Set the "Cookie:" header back to the parsed/filtered value set req.http.cookie = cookie.get_string(); }

Slide 47

Slide 47 text

CHALLENGE: HOST sub vcl_recv { # domain.tld vs www.domain.tld, force www subdomain if (req.http.host == "domain.tld") { return (synth(720, "http://www.domain.tld")); } } sub vcl_synth { if (resp.status == 720) { set resp.http.Location = resp.reason; set resp.status = 301; return (deliver); } }

Slide 48

Slide 48 text

CACHING API REQUESTS sub vcl_hash { hash_data(req.url); // ie: /api/companies/5/products hash_data(req.http.host); // ie: ma.ttias.be if (req.http.Authorization) { hash_data(req.http.Authorization); // ie: Bearer Access-Token } if (req.http.X-Custom-Header) { hash_data(req.http.X-Custom-Header); // ie: app specific } }

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

FLUSHING THE CACHE

Slide 51

Slide 51 text

PURGE REQUEST acl purge { "localhost"; "192.168.55.0"/24; } sub vcl_recv { # allow PURGE from localhost and 192.168.55... if (req.method == "PURGE") { if (!client.ip ~ purge) { return(synth(405,"Not allowed.")); } return (purge); } } $ curl -X PURGE -H "Host: domain.tld" http://31.193.180.217/blog/page1

Slide 52

Slide 52 text

BAN REQUEST $ varnishadm \ -S /etc/varnish/secret \ "ban req.http.host ~ (www.)?domain.tld" $ varnishadm \ -S /etc/varnish/secret \ -T 31.193.180.217:6082 \ "ban req.http.host ~ www.domain.tld && req.url ~ .css" $ cat /etc/varnish/secret yb9exW6O57c77b9T9lHxkLAShsJ-QbXAdB7CMlGegGK1Q5ViJ5NTwRW

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

EDGE SIDE INCLUDES

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

TTL: HOW LONG TO CACHE? header("Cache-Control: public, max-age=900"); sub vcl_backend_response { if (bereq.url ~ "/blog/post-on-hackernews") { set beresp.ttl = 600s; return (deliver); } }

Slide 60

Slide 60 text

GRACE MODE sub vcl_backend_response { # All objects get a 10 minute cache lifetime set beresp.ttl = 10m; # But just in case, keep objects in cache for an extra 20 min # Can be served to clients while fetching new content # Total time object can be in the cache: 10min + 20min = 30min set beresp.grace = 20m; }

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

varnishhist

Slide 63

Slide 63 text

varnishlog

Slide 64

Slide 64 text

varnishncsa

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

GETTING STARTED

Slide 67

Slide 67 text

THANK YOU!