Cache a specific URL with Varnish

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.