Skip to content

Varnish and CDNs

Shane Rounce edited this page Nov 30, 2026 · 6 revisions

Varnish and CDNs

Ace Redis Cache is the cache nearest to WordPress. Many sites have another in front: Varnish on the same server, or a CDN at the edge. Two layers work well together as long as they agree on when a page has changed.

Varnish

What the plugin does

Every page cache purge is mirrored to Varnish as a ban for the same pages:

In the plugin In Varnish
Specific paths purged A ban for each path, anchored so /news/ does not also ban /news/archive/
Everything cleared The whole host is banned
Soft purge The whole host is banned
A background refresh stores a new copy of a page That page is banned, so visitors get the new copy at once

Settings

On the Caching tab, under Edge and Proxy Caches:

Setting Meaning
Mode Automatic (the default), on or off
Servers Up to ten host:port addresses. The default is 127.0.0.1:6081.
Test Checks each server answers
Flush Bans the whole site in Varnish
Last purge The result of the most recent purge sent to each server

The Edge and Proxy Caches section of the Caching tab, with the header switches and Varnish settings

In automatic mode the plugin looks for Varnish on 127.0.0.1:6081, at most once every ten minutes, and does nothing if it is not there.

The same from the command line: wp aceredis varnish status, test and flush.

Constants in wp-config.php win over the settings:

define( 'ACEREDIS_VARNISH', true );              // or false, or 'auto'
define( 'ACEREDIS_VARNISH_HOST', '10.0.0.5:6081' );

Filters: aceredis_varnish_enabled, aceredis_varnish_hosts and aceredis_varnish_ban_on_refresh. To ban the whole site from your own code:

do_action( 'aceredis_cache_varnish_ban_site' );

The plugin never changes server configuration. Your Varnish must be set up to accept PURGE from the web server, as below.

What your VCL needs

The plugin sends a PURGE request whose target is a regular expression. Your VCL must turn that into a ban for the host, and accept PURGE from the web server:

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"));
    }
}

Two additions I recommend:

sub vcl_recv {
    # Background refreshes must reach PHP, not be answered from Varnish.
    if (req.http.X-AceRedis-Prime) {
        return (pass);
    }
}

sub vcl_backend_response {
    # Do not keep a stale copy for long: the fresh one is seconds away.
    if (beresp.http.X-AceRedisCache ~ "stale") {
        set beresp.ttl = 10s;
    }
}

Reading headers through Varnish

Varnish stores the response headers along with the page. An X-AceRedisCache: MISS seen through Varnish may be a week old: it is the header from the request that filled Varnish, replayed. To see what the origin is doing, ask for a URL Varnish has not stored, or send the request to the web server directly.

Public text fragments

From 0.12.0, Varnish can refresh a short public value, such as opening hours, while keeping the surrounding page cached. Register a slug, a plain-text callback and a lifetime up to 3,600 seconds with aceredis_public_fragments. Print the result with PublicFragments::render() from the AceMedia\RedisCache namespace. Visitors who reach the origin directly receive the text inline.

Configure Varnish and the origin with the same dedicated secret in ACEREDIS_PUBLIC_ESI_TOKEN. The release example sets out the canonical backend, trusted requests, and how to invalidate a changed fragment through the purge bridge. Keep the callback independent of the visitor. Integration example

In 0.13.1, open Settings → Ace Redis Cache → Caching → Varnish and choose Serve registered public text fragments through Varnish ESI. Installations already running with a valid token keep their earlier behaviour until an explicit choice is saved. The checkbox alone cannot enable assembly without the token and proxy configuration.

ARC Varnish settings with the public edge fragments switch

Watch the switch being turned off and on

Clearing the checkbox retires shared fragment URLs at the origin. ARC also queues a site ban at the configured Varnish. Check that the proxy accepted it. If necessary, purge any outer cache with its own controls. An ordinary stored page may still contain earlier inline text until the page is refreshed. When you re-enable ESI, a cold response can show the inline value before Varnish receives a cacheable shell.

CDNs

Two layers work when they agree on what has changed. The plugin gives a CDN three things: headers it can cache by, tags it can be purged by, and a hook that fires when something changes. For Cloudflare it can also do the purging itself.

Everything in this section is off by default, and none of it contacts an outside service unless you switch on the Cloudflare integration.

Cache tag headers

Turn on Cache tag headers and every cached page is sent with Cache-Tag and Surrogate-Key headers naming what is on it.

Page Tags
A single post or page post-<id>, type-<type>, author-<id>, term-<id> for each term
A listing archive-<type>, archive-term-<id>, archive-author-<id>, home, feed or search as fits, plus post-<id> for each post shown
Every page One tag for the whole site

Every tag starts with a short code for the site, so two sites behind one edge cache never share a tag.

When a post changes, the plugin fires an action with the tags for that post and the listings that may show it. Purge exactly those:

add_action( 'aceredis_cache_tags_invalidated', function ( $tags, $post_id ) {
	my_cdn_purge_tags( $tags );
}, 10, 2 );

CDN-Cache-Control

A separate switch sends CDN-Cache-Control with the shared lifetime, for edges that read that header in preference to Cache-Control.

Cloudflare

Optional, and off by default. In the Cloudflare section of the Caching tab, switch the integration on and enter:

  • your zone ID;
  • an API token with the Cache Purge permission for that zone. Add Zone Read if you want the Test button to confirm the zone.

From then on, whenever the plugin invalidates pages it asks Cloudflare to purge the same URLs, or the same cache tags when cache tag headers are on. The requests are sent from your server after the page has been delivered, so no visitor waits on them.

A full clear purges what Cloudflare holds for your host name. It never purges the whole zone, because a zone can hold other sites. To allow that:

add_filter( 'aceredis_cloudflare_allow_purge_everything', '__return_true' );

To keep the token out of the database, define it in wp-config.php:

define( 'ACEREDIS_CLOUDFLARE_ZONE', 'your-zone-id' );
define( 'ACEREDIS_CLOUDFLARE_TOKEN', 'your-api-token' );

What is sent to Cloudflare: your zone ID, your token, and either the URLs or tags that were invalidated or, on a full clear, your host name. No visitor data, cookies or page content. Nothing is sent while the integration is off.

Any other CDN

Use the cache tags above, or purge by path:

add_action( 'aceredis_paths_invalidated', function ( $paths ) {
	my_cdn_purge_paths( (array) $paths );
} );

add_action( 'aceredis_cache_cleared', 'my_cdn_purge_everything' );
add_action( 'aceredis_page_epoch_bumped', 'my_cdn_purge_everything' );

Letting the CDN cache pages

Turn on Browser Cache Headers and hits are sent with Cache-Control: public with a max-age and an s-maxage. Only do this if the CDN is purged when pages change, or you are happy for the edge to serve a page until its lifetime ends.

Things that go wrong behind a CDN

Symptom Cause Fix
Live blocks stop refreshing, or pages with them always go to WordPress The background render calls the site by its public name and the CDN or its firewall turns it away define( 'ACEREDIS_EARLY_REFRESH_ADDR', '127.0.0.1' );
Separate http and https copies, or redirects stored in the wrong scheme The origin does not know the visitor used HTTPS Have the CDN or proxy send X-Forwarded-Proto: https. The drop-in reads it.
Mobile visitors get the desktop page The CDN caches one copy for all devices while the plugin keeps two Vary the CDN cache by device, or serve the same markup to both
Signed-in users see guest pages The CDN is caching pages for requests that carry WordPress cookies Bypass the CDN cache when a wordpress_logged_in_ cookie is present
The header says MISS on every request You are reading a header the CDN stored Check the origin directly

Campaign links

Tracking parameters such as utm_source, gclid and fbclid are dropped from the cache key, so a paid landing is served the same stored page as the clean URL. If your CDN can ignore the same parameters, set it to, and both layers benefit. The list is on Page cache.

Use a dynamic WordPress block

Add a block name to an explicitly registered fragment, for example 'block' => 'my-store/opening-hours'. ARC checks that the dynamic block has no attributes, context or inner blocks and renders the registered public text. A single paragraph, span or div around that text keeps its wrapper. Other markup uses the ordinary block rendering. Direct visitors receive the inline public value. Registration and Varnish configuration

Clone this wiki locally