Caching only images, no cookies
David Murphy
david at firechaser.com
Wed Feb 2 17:40:48 CET 2011
Thanks for this Per.
Following your adbvise I have got the following in my .vcl ( I use
sudo /usr/local/sbin/varnishd -a :80 -f
/usr/local/etc/varnish/default.vcl -s malloc,1G to start Varnish)
//START
backend default {
.host = "127.0.0.1";
.port = "8080"; //Apache listening
.connect_timeout = 600s;
.first_byte_timeout = 600s;
.between_bytes_timeout = 600s;
}
sub vcl_recv {
if (req.url ~ "\.(png|gif|jpg)$") {
remove req.http.Cookie;
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_error {
set obj.http.Content-Type = "text/html; charset=utf-8";
synthetic {"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head></head><body>error message goes here</body></html>
"};
}
//END
When I visit a page (of a Drupal powered site) that is delivered via
Varnish, the XHTML is not cached (good) and when I view an image in
separate browser tab and check firebug I get
X-Varnish 1355426855 1355414030
Age 1135
Via 1.1 varnish
Connection keep-alive
X-Cache HIT
Which looks good too :).
I can see cookie data in the request headers, but no cookie data in
the response headers. Example page:
http://web4.v1.southbank.signoff.info/visitor-info
So given the above, can I be confident that the only thing Varnish
will cache is anything that does not have a cookie ( i.e. what is
caches sensibly by default, plus the images inclusion I've added)?
I'm going to start playing with the CLI now to understand a bit more
about what's actually in the cache. Is there an easy way to see if the
cache contains anything with a cookie? That would be a useful sanity
check for me to run from time to time as I develop the .vcl.
Many thanks - am really pleased with how easily and quickly I was able
to get this running.
Best, David
On Wed, Feb 2, 2011 at 9:49 AM, Per Buer <perbu at varnish-software.com> wrote:
> David,
>
> On Wed, Feb 2, 2011 at 10:14 AM, David Murphy <david at firechaser.com> wrote:
>> Morning Per
>>
>> So the default behaviour of Varnish (only extended if I start writing
>> my own .vcl) would make a smart choice about what should be cached?
>> That sounds good. So provided that I strip the cookies for the image
>> file types, then Varnish will spot that these request do not have any
>> cookies and will safely cache them, ignoring all other requests (since
>> I've not stripped their cookies).
>
> Right.
>
>> Something as simple as
>>
>> sub vcl_recv {
>> if (req.url ~ "\.(png|gif|jpg)$") {
>> remove req.http.Cookie;
>> }
>> }
>
> Right. Provided your backend produces sane cache-control headers. If
> there is no information on how long to cache it Varnish will cache for
> a default period of 120 seconds. You can override the TTL -
> http://www.varnish-cache.org/docs/trunk/tutorial/increasing_your_hitrate.html#overriding-the-time-to-live-ttl
>
>> ...and nothing else in .vcl file?
>
> No. That should be sufficient.
>
>
> --
> Per Buer, Varnish Software
> Phone: +47 21 98 92 61 / Mobile: +47 958 39 117 / Skype: per.buer
> Varnish makes websites fly!
> Want to learn more about Varnish? http://www.varnish-software.com/whitepapers
>
More information about the varnish-misc
mailing list