I'm struggling to understand the behavior of varnish and was hoping some one can shed some light.
I'm doing a test where I'm trying to make varnish to cache requests/responces with cookies.
I have a very simple PHP script, it simple start a session.
<?php session_start(); ?>
I do expect varnish not to cache due Set-Cookie and Cookie headers.
I'll go and unset these headers:
sub vcl_backend_response {
unset beresp.http.set-cookie;
}
sub vcl_recv {
unset req.http.cookie;
}
The requested page is still not being cached.
I know that PHP will send a cache-busting headers which might be respected by varnish. Let's check:
<?php echo session_cache_limiter(); ?>
Output: nocache
From session_cache_limiter() documentation it is known that nocache wills send these responce headers to bust the cache:
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Let's unset them:
sub vcl_backend_response {
unset beresp.http.expires;
unset beresp.http.pragma;
unset beresp.http.cache-control;
unset beresp.http.set-cookie;
}
As this point I would expect varnish to cache, however still no cache.
The fix which worked is to set a ttl:
sub vcl_backend_response {
unset beresp.http.expires;
unset beresp.http.pragma;
unset beresp.http.cache-control;
unset beresp.http.set-cookie;
# THIS MADE VARNISH TO CACHE
set beresp.ttl = 10m;
}
And now the question:
Why would I need to set TTL explicitly if the default value is 2m (-t 120 in the CLI below) and all cache-busting headers are taken care of?
/usr/sbin/varnishd -P /var/run/varnish.pid -f /etc/varnish/default.vcl -a :80 -T 0.0.0.0:6082 -t 120 -u varnish -g varnish -S /etc/varnish/secret -s malloc,512M
Am I missing something? Is there a different way, whitout setting TTL (which I would prefer)?