13

Varnish is being used in a Wordpress website.

I would like to purge Varnish for a single URL instead of a whole domain.

With this command I can restart Varnish for the whole domain:

varnishadm -T :6082 -S /etc/varnish/secret 'ban req.http.host ~ \"http://www.foo.com\" && req.url ~ \"^/\"'

However I would like to purge varnish only for a single url.

Ex: www.foo.com/url_to_be_purged

I've tried the previous command replacing it with the single URL:

varnishadm -T :6082 -S /etc/varnish/secret 'ban req.http.host ~ \"http://www.foo.com/url_to_be_purged\" && req.url ~ \"^/\"'

But it didn't work, the URL still was a HIT in Varnish.

Any ideas of how can I achieve this?

UPDATE

As suggested ghloogh's answer, I've tried the following command:

varnishadm -T :6082 -S /etc/varnish/secret ban "req.http.host == http://www.foo.com && req.url == http://www.foo.com/url_to_be_purged"

I've also tried this variation:

varnishadm -T :6082 -S /etc/varnish/secret ban "req.http.host == http://www.foo.com && req.url == /url_to_be_purged"

But I still get a HIT in the URL and the data is not updated

2
  • 1
    man, stop using scheme (http:// or https:// or ftp:// or other) in hostname :) your command should be: varnishadm -T :6082 -S /etc/varnish/secret ban "req.http.host == www.foo.com && req.url == /url_to_be_purged"
    – ghloogh
    Jul 11, 2013 at 15:42
  • How can we purge URL by pattern, say all matches for /foo/bar/ab*
    – anup
    Jun 11, 2021 at 6:58

2 Answers 2

20

You don't need to specify scheme for hostname and you may use strict match instead of regex:

varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret ban "req.http.host == example.com && req.url == /some/url/"
1
  • 1
    The other answer had the quote in the wrong place. > varnishadm -T 127.0.0.1:6082 -S /etc/varnish/secret "ban req.url == /url_to_be_purged"
    – Jim Holmes
    Oct 21, 2016 at 12:00
2

You can try the following:

sudo varnishadm -S /etc/varnish/secret  -T 127.0.0.1:6082 "ban req.url ~ \"http://example.com/testimage.png$\""

worked for me.

3
  • 2
    This doesn't add anything but sudo to the other answer. This might be better as a comment. Jun 26, 2017 at 12:52
  • you saved my day! To solve it is necessary to consider the slashes before the quotes May 12, 2020 at 8:44
  • The difference for me was the idea of using the $ in the regex to ensure no other URLs containing the one to clear gets wiped out. The other answer requires you know the domain to clear. For me I don't know the domain, just the URI fragment so this method works well for me. Oct 11, 2022 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.