I'm new to Varnish Cache and have a question I hope I can get some help with.
I have a very simple and basic setup but it's not working as I understand it should for some reason.
It's related to Varnish not caching PHP pages that are using cookies.
Here is my setup:
1) For my default.vcl I have a simple backend
backend default {
.host = "127.0.0.1";
.port = "80";
}
2) I have a simple PHP file that has only these two line:
session_start();
echo time();
3) When I call this page it correctly does not cache as I have not added in the required vcl rules
4)
So as per my understanding of the documentation I add in these two rules
sub vcl_recv {
unset req.http.Cookie;
return (lookup);
}
sub vcl_fetch {
unset beresp.http.Set-Cookie;
return(deliver);
}
5)
The PHP page still will not cache.
I can see the Set-Cookie
header has been
removed as I'm using FireBug in FireFox.
It's only if I add this to sub vcl_fetch that the PHP will cache:
set beresp.ttl = 24h;
My question is is this correct?
I didn't think I would need to alter the ttl of the backend response. I thought just unsetting cookies in and out would force PHP w/ session to cache.
My complete default vcl is:
backend default {
.host = "127.0.0.1";
.port = "80";
}
sub vcl_recv {
unset req.http.Cookie;
return (lookup);
}
sub vcl_fetch {
unset beresp.http.Set-Cookie;
set beresp.ttl = 24h;
return(deliver);
}
My start up command is:
varnishd -f /etc/varnish/default.vcl -s malloc,128M -T 127.0.0.1:2000 -a 0.0.0.0:8080
The URL I am calling is:
http://vbox.local:8080/varnish-tests/index.php
My index.php file has only:
<?php
session_start();
echo time();
I would like to ask the community if this looks correct or if I am wrong. Essentially I'm just unsure why I had to add the beresp.ttl = 24h to finally have the page cache in varnish.
I thought I would not need this.
Any advise much appreciated.
Thank you!
Kind regards.
Varnish will obey the caching headers on the response. PHP will send cache-control headers NOT to cache the response by deafult
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma:no-cache
You need to disable those headers otherwise Varnish will obey and thus not cache the page. To turn them off simply call session_cache_limiter() with an empty string
session_cache_limiter('');
header("Cache-Control: public, s-maxage=60");
session_start();
You can then add a header to set the cache-control to public. Using the three lines above will enable caching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With