Caching only images, no cookies

David Murphy david at firechaser.com
Wed Feb 9 16:14:24 CET 2011


Hello

I've been testing removing cookies from images/css/js and am a little
unclear on the difference between:


//start ==========
 sub vcl_recv {
  if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
        remove req.http.Cookie;
   }
//end==========


and...


//start =========
sub vcl_recv {
  if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
    unset req.http.cookie;
  }
}

sub vcl_fetch {
  if (req.url ~ "\.(png|gif|jpg|swf|css|js)$") {
    unset beresp.http.set-cookie;
  }
}
//end =========


The first example (as suggested by Per below) removes the cookie from
the incoming requests for these files so that these objects are
cacheable.

What I'm a bit unclear on is why the 2nd example uses 'unset' in both
vcl_recv and also when the object has been retrieved from the backend
(vcl_fetch).

I need to be 100% sure that for all image/css objects, there are no
cookies stored anywhere. Is this what the first example does, whereas
the second example 'ignores' the cookie?

Thanks for any help.

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