requête Symfony ✘ Les annotations du SensioFrameworkExtraBundle ✘ Un peu de code: ✗ Use Case 1: headers spéciaux pour piloter varnish ou un CDN ✗ Use Case 2: conversion de cookie pour l'utilisation des vary ✗ Use Case 3: découper votre page en ESI ✘ Points d'attention: ✗ Effet google, stratégie stale content ✗ Mesure d'efficacité du cache ✗ Résolution séquentielle des ESI Varnish
response headers from the backend. # # Here you clean the response headers, removing silly Set-Cookie headers # and other mistakes your backend does. # Serve stale version only if object is cacheable if (beresp.ttl > 0s) { set beresp.grace = 1h; } # Objects with ttl expired but with keep time left may be used to issue conditional (If-Modified- Since / If-None-Match) requests to the backend to refresh them #set beresp.keep = 10s; # Custom headers to give backends more flexibility to manage varnish cache if (beresp.http.X-Cache-Varnish-Maxage) { set beresp.ttl = std.duration(beresp.http.X-Cache-Varnish-Maxage + "s", 3600s); } if (beresp.http.X-Cache-Varnish-Grace && beresp.ttl > 0s) { set beresp.grace = std.duration(beresp.http.X-Cache-Varnish-Grace + "s", 3600s); } }
Page totalement différente suivant que l’ utilisateur est authentifié ou non. ✘ Contenu de la page lié aux permissions de l’ utilisateur. Mais : ✘ Toutes les versions de page doivent être en cache avec une même URL.
Reset the header to ensure it isn't defined by the client. set req.http.X-Roles = ""; # Checks if the permission cookie is present and correctly formatted if (req.http.cookie ~ "roles=([^-;]+)-([^-;]+)-([^-;]+)") { set req.http.cookieValue = regsub(req.http.cookie, ".*roles=([^-;]+)-([^-;]+)-([^-;]+).*", "\1"); set req.http.cookieExpiry = regsub(req.http.cookie, ".*roles=([^-;]+)-([^-;]+)-([^-;]+).*", "\2"); set req.http.cookieSign = regsub(req.http.cookie, ".*roles=([^-;]+)-([^-;]+)-([^-;]+).*", "\3"); # Checks the expiration date contained in the cookie if (std.integer(req.http.cookieExpiry, 0) > std.time2integer(now)) { # Checks the signature of the cookie to ensure it was not modified by the client if ("0x"+req.http.cookieSign == digest.hmac_sha256("<secret>", req.http.cookieValue+"-"+req.http.cookieExpiry)) { set req.http.X-Roles = req.http.cookieValue; } } unset req.http.cookieValue; unset req.http.cookieExpiry; unset req.http.cookieSign; } unset req.http.cookie; VCL Varnish 4 (sans doute améliorable)
✘ X-Roles : pour que Varnish stocke une page différente pour chaque valeur de ce header ✘ Cookie : pour “invalider” le cache navigateur lors d’un changement de rôles (connexion) ✘ Accept-Encoding : dépend du support de gzip Lire aussi : https://www.fastly.com/blog/best-practices-for-using-the-vary-header HTTP/1.1 200 OK Cache-Control: max-age=86400 Content-Encoding: gzip Vary: Accept-Encoding,X-Roles,Cookie
dans : ✘ Varnish ✘ Akamai ✘ Apache Traffic Server ✘ Symfony HttpCache Utile pour : ✘ TTL différentes pour chaque partie d’une page ✘ Assembler les réponses de plusieurs “micro-services” https://www.akamai.com/us/en/support/esi.jsp C’est vous qui choisissez qui assemble la page : ✘ L’application ✘ Le serveur ✘ Le CDN