Bulgaria PHP Conference, September 25th, 2015
apt-get install varnish
apt-get install varnish
/etc/default/varnish, replace port 6081 by port 80
service apache2 restart
service varnish restart
httpstatusdogs.com
GET /path
HTTP/1.1 200 OK
Content-Type: text/html
<html>...</html>
twitter.com/stevelosh/status/372740571749572610
wget -Sq --spider varnish.lo/solutions/expiration.php
curl -o /dev/null -sD - varnish.lo/solutions/expiration.php
HTTP 1.1, RFC 2616, Sections 13.2 and 13.3
Cache-Control: s-maxage=3600, max-age=900
Expires: Thu, 15 May 2014 08:00:00 GMT
curl -sD - varnish.lo/noinfo.php
curl -sD - varnish.lo/exercises/expiration.php
ETag: 82901821233
If-None-Match: 82901821233
304 Not Modified
curl -sD - varnish.lo/exercises/etag.php
curl -H "If-None-Match: abc" -sD - varnish.lo/exercises/etag.php
Last-Modified: Tue, 13 May 2014 08:13:20 GMT
If-Modified-Since: Tue, 13 May 2014 08:13:20 GMT
304 Not Modified
curl -sD - varnish.lo/exercises/last-modified.php
curl -H "If-Modified-Since: {previous timestamp}" -sD - varnish.lo/exercises/last-modified.php
gmdate('r') does not work as timezone is output as "+0000" instead of "GMT".
Cache-Control: s-maxage=0, private, no-cache
www.varnish-cache.org/trac/browser/bin/varnishd/default.vcl?rev=3.0 (Varnish 3)
www.varnish-cache.org/trac/browser/bin/varnishd/builtin.vcl?rev=4.0 (Varnish 4)
curl -sD - varnish.lo/exercises/nocache.php
curl -sD - varnish.lo/cookie.php
curl -H "Cookie: x" -sD - varnish.lo/solutions/expiration.php
GET /resource
Accept: application/json
GET /resource
Accept: text/xml
Vary: Accept
curl -sD - varnish.lo/exercises/content-negotiation.php
curl -H "Accept: application/json" -sD - varnish.lo/exercises/content-negotiation.php
Think carefully and test thoroughly
sub vcl_backend_response {
set beresp.http.X-TTL = beresp.ttl;
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
backend default {
.host = "127.0.0.1";
.port = "8080";
}
backend legacy {
.host = "127.0.0.1";
.port = "8000";
}
sub vcl_recv {
if (req.url ~ "^/archive/") {
set req.backend_hint = legacy;
} else {
set req.backend_hint = default;
}
}
sub vcl_recv {
if (req.http.Cookie) {
if (req.url ~ "^/cache") {
remove req.http.Cookie;
}
}
}
curl -H "Cookie: x" -sD - varnish.lo/solutions/expiration.php
header('X-Reverse-Proxy-TTL: 30');
C{ #include <stdlib.h>; }C
sub vcl_backend_response {
if (beresp.http.X-Reverse-Proxy-TTL) {
C{
char *ttl;
const struct gethdr_s hdr = { HDR_BERESP, "\024X-Reverse-Proxy-TTL:" };
ttl = VRT_GetHdr(ctx, &hdr);
VRT_l_beresp_ttl(ctx, atoi(ttl));
}C
unset beresp.http.X-Reverse-Proxy-TTL;
}
}
...