Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP + Caching - PHPUG Cologne 2014/03

HTTP + Caching - PHPUG Cologne 2014/03

A talk about HTTP headers and how they influence cache behavior.

Avatar for Tobias Gies

Tobias Gies

March 07, 2014
Tweet

More Decks by Tobias Gies

Other Decks in Programming

Transcript

  1. HTTP request GET / HTTP/1.1 Host: www.example.com Connection: keep-alive Cache-Control:

    no-cache Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Pragma: no-cache User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4 Cookie: sid=t73fuqi322d115fdr8tt1fsf30
  2. HTTP response HTTP/1.1 200 OK Date: Thu, 20 Feb 2014

    14:42:03 GMT Server: Apache/2.2.22 (Ubuntu) PHP/5.3.10-1ubuntu3.9 mod_ssl/2.2.22 OpenSSL/1.0.1 X-Powered-By: PHP/5.3.10-1ubuntu3.9 P3P: CP="NOI NID ADMa OUR IND UNI COM NAV" Set-Cookie: sid=t73fuqi322d115fdr8tt1fsf30; path=/; HttpOnly Expires: Sun, 22 Dec 2013 14:42:03 GMT Cache-Control: no-store, no-cache, must-revalidate, no-transform Last-Modified: Thu, 20 Feb 2014 14:42:03 GMT X-Generator: papaya CMS X-Papaya-Cache: no Content-Encoding: gzip X-Papaya-Gzip: yes Content-Length: 6315 Keep-Alive: timeout=5, max=95 Connection: Keep-Alive Content-Type: text/html; charset=utf-8
  3. Cache-Arten • Browser-Cache • Proxy Cache • Gateway Cache /

    Reverse Proxy – Self-hosted (Varnish, …) – Kommerziell / CDN (Cloudflare, Akamai & co.)
  4. Cache-relevante HTTP-Header • Expires • Cache-Control • ETag • Last-Modified

    • If-Modified-Since • If-None-Match • Vary • Age • Set-Cookie
  5. Expires Expires: Sun, 22 Dec 2013 14:42:03 GMT • HTTP/1.0

    • Ablaufdatum des Inhalts (Zeitzone GMT!) • Nützlich für Caching statischer Ressourcen
  6. Cache-Control Cache-Control: no-cache, must-revalidate • HTTP/1.1 • Wesentlich feinere Kontrolle

    über den Cache • Optionen: – max-age – s-maxage – public – private
  7. Cache-Control Cache-Control: no-cache, must-revalidate • HTTP/1.1 • Wesentlich feinere Kontrolle

    über den Cache • Optionen (2): – no-cache – no-store – must-revalidate – proxy-revalidate
  8. ETag ETag: [W/]"some-random-value" • HTTP/1.1 • Identifiziert eine Version einer

    Ressource eindeutig • Starker ETag: Garantiert identische Ressource • Schwacher ETag (W/): Garantiert äquivalente Ressource • Bei statischen Ressourcen abschalten
  9. If-Modified-Since If-Modified-Since: Wed, 01 Sep 2004 13:24:52 GMT • Request-Header

    • Nur ausliefern, wenn neuere Version vorhanden • Sonst Status 304
  10. Vary Vary: Cookie,Accept,Accept-Encoding • Information an Caches: Antwort unterscheidet sich

    je nach Wert der angegebenen Request- Header! • Bspw. wichtig im Umgang mit schlecht konfigurierten Corporate Proxies
  11. Age Age: 30 • Gibt das Alter der Ressource in

    Sekunden an • Caches rechnen: request timestamp – now – Age + max-age > 0? • Wird auch von Caches selbst angegeben
  12. Set-Cookie Set-Cookie: name=somevalue; Secure • Inhalt, der Cookies enthält, ist

    uncached! • Aber: Cookies sind nützlich, um mehrere Versionen einer Ressource im Reverse-Proxy zu cachen.
  13. POST • POST bedeutet: Es wird eine Ressource verändert. •

    Antworten auf POST-Requests werden nicht gecached. • POST also nur da verwenden, wo es nötig ist.
  14. Ressourcen • Wikipedia: „Liste der HTTP-Headerfelder“ • Caching Tutorial for

    web authors and webmasters: http://www.mnot.net/cache_docs/ • HTTP – The Varnish Cookbook: https://www.varnish-software.com/static/book/HTTP.html • YSlow: Best practices for speeding up your website: http://developer.yahoo.com/performance/rules.html