Varnish Caching Proxy
Madison PHP Conference
September 13th, 2014
© David Buchmann, Liip AG
Step 1
apt-get install varnish
Step 2
- Edit
/etc/default/varnish
, replace port 6081 by port 80
- Edit webserver config to run on port 8080
service apache2 restart
service varnish restart
Step 3
What could possibly go wrong?
- Nothing gets cached (HTTP Headers)
- Too much gets cached (also HTTP Headers)
- Editors see no changes (cache invalidation)
- Caches get mixed up (personal content)
What is a reverse proxy again?

HTTP is simple
GET /path
HTTP/1.1 200 OK
Content-Type: text/html
<html>...</html>
HTTP verbs
- GET
- HEAD
- POST
- PUT
- DELETE
- ...
HTTP response codes
- 1xx hold on
- 2xx here you go
- 3xx go away
- 4xx you fucked up
- 5xx I fucked up
https://twitter.com/stevelosh/status/372740571749572610
Default Varnish behaviour
- Only attempt to cache GET and HEAD request
- Never cache request with cookies / authorization
- Never cache response with set-cookie
Only cache response with status 200, 203, 300, 301, 302, 307, 404, 410
http://httpstatusdogs.com
Cache control headers
Cache-Control: s-maxage=3600, max-age=900
Expires: Thu, 15 May 2014 08:00:00 GMT
- s-maxage
- max-age
- Expires
- Default to default_ttl if nothing specified
Do not cache
Cache-Control: s-maxage=0, private, no-cache
- Varnish 3 does not respect no-cache by default, but s-maxage=0.
- Varnish 4 respects both s-maxage=0 and no-cache.
Keep variants apart
Request
Accept: application/json
Response
Vary: Accept
Cache validation (the new way)
ETag: 82901821233
If-None-Match: 82901821233
304 Not Modified
