Varnish · 30 November 2026

Varnish in front of a WordPress page cache: purge both or serve old pages

Two caches holding the same page is a good set-up until somebody edits the page. Then the inner cache updates and the outer one carries on serving the old copy. Here are the rules that keep Varnish and a WordPress page cache in step.

By Shane Rounce · 5 minute read

The problem: the edit that does not show

You publish a correction. Signed in, you see it. Signed out, the old page is still there, and it stays there for as long as Varnish chooses to keep it.

The page cache inside WordPress knew about the edit and replaced its copy. Varnish, sitting in front, was never told. Two layers are only as fresh as the one that hears about changes last.

Is it worth running both?

Yes, when each does a different job.

LayerServesGood at
VarnishSigned-out visitors and crawlersAnswering repeat requests without touching PHP at all, and carrying on when the origin is slow
Page cache in RedisWhatever Varnish does not haveKnowing exactly which pages an edit affects, and rebuilding them
Object cache in RedisSigned-in visitors, admin, background jobsSaving database work on pages that cannot be cached whole

If you are unsure about the bottom two rows, page cache and object cache explains them.

Rule one: purge both, every time

Wherever the application removes a page from its own cache, it must tell Varnish to drop the same page. Varnish needs a way to accept that, limited to the web server itself:

acl purge {
    "localhost";
    "127.0.0.1";
}

sub vcl_recv {
    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            return (synth(405, "Not allowed"));
        }
        ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);
        return (synth(200, "Banned"));
    }
}

Three details matter here.

  • Match the host as well as the path. One Varnish often fronts several sites. A ban on /news/ without a host clears that path on all of them.
  • Anchor the pattern. A ban for /news/ should not also remove /news/archive/.
  • Send purges once, at the end of the request. A single save can touch a post, its categories, its author page and the home page. Collect the paths, remove duplicates, and collapse to one site-wide ban when the list covers everything.

Rule two: never store what is not yours to share

The worst cache fault is one visitor seeing another's basket or account. Pass personal requests straight through, and refuse to store a response that sets a cookie or says it is private.

sub vcl_recv {
    if (req.http.Cookie ~ "wordpress_logged_in_|wp-postpass_|comment_author_|woocommerce_") {
        return (pass);
    }
    if (req.url ~ "^/(wp-admin|wp-login\.php|cart|basket|checkout|my-account)") {
        return (pass);
    }
}

sub vcl_backend_response {
    if (beresp.http.Set-Cookie || beresp.http.Cache-Control ~ "private|no-store|no-cache") {
        set beresp.uncacheable = true;
        return (deliver);
    }
}

Use your shop's real basket and account paths, not only the defaults. Cache the page, not the visitor covers what is safe to share in the first place.

Rule three: let refreshes through

A page cache that rebuilds pages in the background does it by requesting them. If that request is answered by Varnish, nothing is rebuilt: the old page is fetched and stored again as if it were new.

Give background refreshes a header and pass them:

sub vcl_recv {
    if (req.http.X-AceRedis-Prime) {
        return (pass);
    }
}

Better still, send the refresh straight to the web server so it never meets the outer layers. In Ace Redis Cache that is define( 'ACEREDIS_EARLY_REFRESH_ADDR', '127.0.0.1' );. Without it the refresh goes out by the site's public name, and a CDN or firewall in front may turn it away.

Rule four: do not let Varnish keep a stale copy for long

If the inner cache is serving stale while it rebuilds, the stale page is only correct for a few seconds. Varnish should not then store it for an hour.

sub vcl_backend_response {
    if (beresp.http.X-AceRedisCache ~ "stale") {
        set beresp.ttl = 10s;
    }
}

Rule five: keep browsers out of it

You can purge Varnish. You cannot purge a visitor's browser. Let the shared caches hold HTML for a long time and tell browsers to hold it briefly or not at all, so an edit reaches people on their next page view.

What Ace Redis Cache does for you

The plugin mirrors every page cache purge to Varnish:

In the pluginIn Varnish
Specific paths purgedA ban for each path, anchored
Everything clearedThe whole host is banned
A background refresh stores a new copyThat page is banned, so visitors get the new copy at once

Varnish is looked for on 127.0.0.1:6081, at most once every ten minutes, and a site with no Varnish there is unaffected. ACEREDIS_VARNISH turns the integration on, off or back to automatic, and ACEREDIS_VARNISH_HOST points it somewhere else. The VCL above is what the plugin expects, and the Varnish and CDNs wiki page has the full version.

The plugin does not talk to any CDN. It gives you actions to hook your own purge to, which the same wiki page lists.

A trap when reading headers through Varnish

Varnish stores the response headers along with the page. An X-AceRedisCache: MISS seen through Varnish may be days old. It is the header from the request that filled Varnish, replayed.

To see what the origin is doing now, ask it directly:

curl -sI -H 'Host: example.com' http://127.0.0.1:8080/ | grep -i x-ace

Use whichever port your web server listens on behind Varnish. HIT, MISS or BYPASS explains what comes back.

Test the whole chain

  1. Load a page signed out, twice. The second response should come from Varnish (an Age header above zero).
  2. Edit the page and save.
  3. Load it signed out again. You should see the edit straight away.
  4. Load a listing that includes the page, such as its category. That should be updated too.
  5. Add something to a basket and load the same page. It must not come from the shared cache.
  6. Clear the whole cache from WordPress and check that Varnish was emptied as well.

Checklist

  • Every application purge is mirrored to Varnish, with the host.
  • Personal requests and cookie-setting responses are never stored.
  • Background refreshes reach PHP.
  • Stale copies get a short lifetime in Varnish.
  • Browsers are not allowed to hold HTML for long.
  • Headers are read at the origin when you are debugging.

For where each layer sits, see how it works and features.

All notes Get the latest release