I have a bit of trouble getting varnish to cache correctly.
When I go to a page and hit refresh , varnish will return the cached page. But if I use another computer
This is because of Google Analytics. Even if your Web application does not use cookies your analytics JavaScript does. The result is the same, Varnish will pass the request to the back-end and avoid using it's cache.
To fix this define URLs of your Web application where you have to use cookies (e.g. administration panel) and where you don't (Here you can ignore the requirements of Google Analytics. Most Web analytics tools need cookies only between the browser and the JavaScript.).
Below you can see an example of a Varnish configuration file for this. The interesting part is that for non-administrative parts of your Web site: Sent cookies will be deleted as well as some browser request headers for fresh content.
sub vcl_recv {
# regex to find all URLs where cookies are required
if (req.url ~ "^/admin/") {
# administration panel
set req.http.admin = 1;
} else {
# public web site, ignore client request for fresh content, remove cookies
unset req.http.Cache-Control;
unset req.http.Max-Age;
unset req.http.Pragma;
unset req.http.Cookie;
}
...
}
sub vcl_fetch {
if (req.http.admin == 1) {
# administration panel
return (hit_for_pass);
} else {
# public web site, not allowed to set cookies
unset beresp.http.Set-Cookie;
...
}
...
}
You can use the varnishlog
command line tool to watch what Varnish is doing as it handles each request. You can see if any cookies are splitting through for example.
I have a feeling it may be the user-agent that is slightly different on the other computer. Varnish includes this value in the key if it is know. We have a rule in our vcl_recv
which is:
set req.http.User-Agent = "";
Which fixed a similar problem for us.