Cache a specific URL with Varnish

10 Jun 2013 in TIL

I wanted to cache one specific route in my application with Varnish, and let everything else through. It took a bit of trial and error, but I finally found a solution that works:

Edit /etc/varnish/default.vcl and set vcl_recv to be:

sub vcl_recv { if (! req.url ~ "^/path/to/cache/") { return(pass); } return(lookup); }

The path to cache uses the tilde operator, which allows you to supply regular expressions.