varnish regex

Michael Alger varnish at
Thu Nov 11 04:05:36 CET 2010

On Wed, Nov 10, 2010 at 11:56:43AM -0500, Vitaly Burshteyn wrote:
> I have a set of url's that I need to make sure that varnish does
> [not] cache.  The url patern looks like this:
> Siteurl/r/anynumber
> if (req.url ~ "/r/*/") {
>         return(pass);
>     }

That looks more like a glob/wildcard match than a regex, and won't
do what you want.

> Or 
> if (req.url ~ "^/r/\d+/$") {
>         return(pass);
>     }

The above is what you want, assuming nothing else can come after the
"anynumber", but there's _always_ a slash after it. If the slash
actually isn't _ever_ present, then just use

  if (req.url ~ "^/r/\d+$") {

If the slash may or may not be present, then

  if (req.url ~ "^/r/\d+/?$") {

may be better. If there's also the possibility of additional things
after the number, such as a query string, and you want to avoid caching
those as well, then I'd just drop the $ at the end:

  if (req.url ~ "^/r/\d+") {

> Sorry about this question, but I am not sure how varnish processes
> regex values.

Varnish switched to pcre in 2.1.x I think; prior to that, regular
expressions were case-insensitive by default. So you should check
what version you're running if that matters to you. Current releases
are case-sensitive by default, so if your backend isn't then you can
use the (?i) syntax to make the expression case-insensitive:

  if (req.url ~ "(?i)^/r/\d+/?$") {

More information about the varnish-misc mailing list