Caching only images, no cookies

David Murphy david at firechaser.com
Wed Feb 2 10:14:26 CET 2011


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).

Something as simple as

sub vcl_recv {
   if (req.url ~ "\.(png|gif|jpg)$") {
        remove req.http.Cookie;
    }
}

...and nothing else in .vcl file?

Thanks, David


On Wed, Feb 2, 2011 at 9:02 AM, Per Buer <perbu at varnish-software.com> wrote:
> Hi David,
>
>
> On Wed, Feb 2, 2011 at 9:55 AM, David Murphy <david at firechaser.com> wrote:
>> Hello
>>
>> I would like to use Varnish to only cache images and to strictly never
>> cache any cookies. Previous attempts at writing the .vcl file led to
>> situations where if an image was missing from the server, the
>> underlying CMS would try to generate a new images (using Imagecache)
>> and this led to session swapping because cookies were cached and the
>> passed onto other users.
>
> Right. Sound bad.
>
>
>> So, I'm looking to have a very simply, reliable script that only cares
>> about images and cannot in any circumstances cache cookies.
>>
>> Would something like this do the trick?
>>
>> sub vcl_recv {
>>  if (req.url ~ "\.(png|gif|jpg)$") {
>>    lookup;
>>  }
>> }
>
> No. Just drop the relevant cookie in vcl_recv. If you strip away the
> cookies the image will be cached by the built in logic. Easy and safe.
>
> --
> 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