Cache a specific URL with Varnish

This article was published 6 years ago. Due to the rapidly evolving world of technology, some concepts may no longer be applicable.

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/") {

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

Thoughts on this post

Thanks for this tip. I assume that by removing the exclamation mark from the IF statement it would cache all routes apart from this one?

michael 2013-11-13

That’s what I’d expect 🙂

MaXe 2014-02-06

An expression such as:
if (req.url ~ “^/crontab\.php$”) {
return (pass);

Will cache everything else except crontab.php

Comments are closed.

Leave a comment?