Disable Varnish Cache Using HTTP Headers

By using Varnish, we can increase web app’s speed significantly. We use Varnish for Cellbazaar. I implemented Pixel Tracking. For Pixel Tracking to work caching should be off for that url. We could change varnish’s configuration to turn off that url’s caching. That seems time consuming and it is against DRY. Turns out we can use HTTP headers to turn off varnish caching.

Here are the header we will use to disbale caching. Expires actually dated one year bac :).

Pragma: no-cache  
Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate 
Expires: Tue, 04 Sep 2012 05:32:29 GMT

In mojlicious,

$self->res->headers->cache_control('private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate');
$self->res->headers->header('Pragma' => 'no-cache');    
$self->res->headers->expires(Mojo::Date->new(time-365*86400));

In php

header('Pragma: no-cache');
header('Cache-Control: private, no-cache, no-store, max-age=0, must-revalidate, proxy-revalidate');
header('Expires: Tue, 04 Sep 2012 05:32:29 GMT');

About Zakir Hyder

This entry was written by .

04. September 2013 by Zakir Hyder
Categories: Caching, Mojolicious, Perl, PHP | Tags: , , , , , , , , , , | Comments

Comments

  1. […] By using Varnish, we can increase web app's speed significantly. We use Varnish for Cellbazaar. I implemented Pixel Tracking. For Pixel Tracking to work caching should be off for that url. We could change varnish's configuration to turn off that…  […]