This had been bugging me a while.

I have defined several rules for emptying varnish caches in addition to the standard stuff by expire/varnish-module.
These include:
- clear frontpage (in every language and alias) every 5 minutes
- clear global cache when certain important nodetypes are saved
- expose a link that allows admin to manually clear varnish caches.

What I wanted was to clear all HTML-pages, but none of the css, js, images etc. I thought it would be easy set this with an regex path clear in rules,
but it wasn't quite so. I searched drupal.org and the internets several hours for a sample PCRE regex for negating strings in varnish. But there was none to be found, except a hint here: http://stackoverflow.com/questions/42990/regex-to-match-against-somethin... .

It was a bit challenging to debug this via varnishadmin, but eventually I got my own version working. This is what I'm using now:
purge.url ^.*(?<!(\\.css|\\.gif|\\.png|\\.jpg|\\.ico|\\.swf|.\\.js))$

A few notes:
* if you copypaste this to rules, you need to leave the start and end char out (^ and $).
* The double escape \\. (used to mark a literal ".") is needed for some reason. I guess the regex string is handled twice.
* .*(?<!( is a "negative lookbehind", which in this case means: the following x letters make this string non-matching.
-> In this case "x" means 4 letters. If all the endings would be 3 letters long (css, gif, png...) I could have spared all that escaping, but since "js" is 2 letters long, I had to match ".js" and the last letter before it. Why? Negative lookbehind requires the string lenghts to be identical.

The end result would look like:
.*(?<!(\\.css|\\.gif|\\.png|\\.jpg|\\.ico|\\.swf|.\\.js))

Would be nice if you could add this to the module documentation somewhere.

Comments

bibo’s picture

Issue summary: View changes

Altered code tag

bibo’s picture

Issue summary: View changes

Added .jpg

bibo’s picture

Status: Active » Needs review

Marking to needs review to get some attention.

bibo’s picture

Issue summary: View changes

minor details