A CDN cache is a copy of your origin’s response, held on an edge server close to the visitor. When a request arrives, that edge server builds a cache key from it — the host, the path, and whichever query strings, headers and cookies you told it to include — looks that key up in its local store, and if it finds a copy whose TTL has not expired, it answers on the spot without ever contacting your origin. That is the entire mechanism. Everything difficult about CDNs sits downstream of two decisions: what goes into the key, and how long each class of response is allowed to live. This page gives you both, as a vendor-neutral rule matrix worked through a WordPress and WooCommerce origin.
What you’ll accomplish
- Say precisely what an edge cache holds, where it lives, and what a cache key is made of.
- Sort every URL on a site into four classes — immutable, short-with-revalidation, private, never — and write one rule per class.
- Keep
/wp-admin/,/wp-login.php,/cart/,/checkout/and/my-account/out of a shared cache deliberately rather than by luck. - Serve stale responses while revalidating, and keep serving during an origin outage, with
stale-while-revalidateandstale-if-error. - Read cache-status headers and a hit ratio, so you can say whether the rules actually worked instead of assuming.
Prerequisites
- A CDN whose rules you can edit. This guide is deliberately vendor-neutral: Cloudflare, Fastly, Akamai, CloudFront and Bunny all expose the same three levers under different names — a match predicate, a cache-key composition, and a TTL-or-bypass action. The field names differ (Cloudflare calls it Edge TTL, Fastly reads
Surrogate-Controland setsberesp.ttlin VCL, CloudFront splits it into a cache policy with min, default and max TTL); the concepts do not. - The ability to set response headers at origin: an nginx
add_header, an ApacheHeader set, or a filter in a WordPress theme or plugin. curl7.x or 8.x for the verification steps. Nothing here needs a recent flag.- The worked examples assume WordPress 6.x with WooCommerce 8.x or newer, on default permalinks and default WooCommerce page slugs. If yours differ, substitute your paths — the classes stay the same.
- One honest limit up front: a CDN cache is a shared cache. Anything you would not show to a stranger must never be stored in it. Two of the rules below exist only to enforce that, and they are the two you check first.
How it works
The cache does not live in one place. Each point of presence keeps its own store, so a HIT in Toronto tells you nothing about Frankfurt, and a freshly purged URL is cold everywhere at once. That per-PoP behaviour — fill rates, tiered caching, why a low-traffic site never warms up — is the subject of how a PoP decides what to hold. Here we care only about the rules that PoP applies.
The cache key. By default it is scheme plus host plus path, and nothing else. Every additional element you key on — a query string, a request header, a cookie — multiplies the number of distinct entries the edge must store and fill for that one URL. The arithmetic is brutal and it is why hit ratios collapse. Key on a country header with 30 values and an Accept-Language with 12, and one URL becomes 360 entries; each one starts life as a MISS, each one ages out on its own schedule, and each one competes for the same finite space in the PoP. Widening the key is the single most common way to turn a working cache into an expensive proxy. Narrow it until something breaks, then add back exactly the one element that broke.
Origin Cache-Control versus the CDN’s own TTL. The origin speaks to two audiences in one header. max-age is the browser’s instruction; s-maxage overrides it for shared caches, which is what your CDN is. So Cache-Control: public, max-age=0, s-maxage=300 means “browser, always revalidate; CDN, hold this for five minutes” — a very common and very useful split. The CDN’s own edge-TTL setting is a third voice, and which one wins is per-vendor: some apply their setting only when the origin sends no Cache-Control at all, others let you override the origin explicitly. Decide it once, write the answer in your runbook, and verify it with curl rather than trusting the dashboard’s wording.
stale-while-revalidate and stale-if-error. These are the two directives that make a short TTL cheap. stale-while-revalidate=60 lets the edge answer instantly with a copy that just expired while it refreshes in the background, so no visitor pays the origin round trip. stale-if-error=86400 lets it keep answering with that stale copy for a day if the origin starts returning 5xx or stops answering. Support varies by CDN, so confirm yours honours them before you rely on them; where they work, they are the difference between a five-minute HTML TTL that helps and one that just moves the stampede around.
Vary. This header tells every downstream cache “the response depends on these request headers, so key on them too”. Vary: Accept-Encoding is correct and expected. Vary: Cookie is almost always a mistake: it declares that the response depends on the entire Cookie header, so every distinct combination of every cookie any visitor carries becomes its own cache entry, and your hit ratio approaches zero within a day of adding an analytics script. Vary: User-Agent is worse, since the cardinality is effectively unbounded. Express a cookie dependency as an explicit bypass rule, not as a Vary.
Step-by-step
1. Find out what the edge does today. Before changing anything, read the headers on one URL from each class. The cache-status header name is vendor-specific, so grep broadly.
curl -sSI https://www.example.com/ \
| grep -iE '^(cache-control|age|vary|etag|surrogate-control|cf-cache-status|x-cache)'
cache-control: no-cache, must-revalidate, max-age=0
vary: Accept-Encoding,Cookie
cf-cache-status: DYNAMIC
That output is the default WordPress posture: nothing is cached, and Vary: Cookie would sabotage it even if something were. It is the starting point for most sites, not a fault.
2. Class 1 — fingerprinted static assets, cached long. An asset is safe to cache for a year only if its URL changes when its bytes change. Build-tool hashes do that; so does WordPress’s ?ver= query string, provided the enqueued version actually gets bumped on each release. Set the header at origin:
location ~* \.(css|js|woff2|svg|png|jpe?g|webp|avif|gif|ico)$ {
add_header Cache-Control "public, max-age=31536000, immutable" always;
access_log off;
}
cache-control: public, max-age=31536000, immutable
age: 41822
cf-cache-status: HIT
immutable tells the browser not to send a revalidation request even on reload. Note the one exception to stripping query strings: for ?ver=-style fingerprints the query string is the fingerprint, so it must stay in the cache key.
3. Class 2 — public HTML, cached briefly and revalidated. Anonymous HTML is the same for every visitor, so it belongs in the cache — just not for long, and never past the moment it changes.
Cache-Control: public, max-age=0, s-maxage=300, stale-while-revalidate=60, stale-if-error=86400
The browser revalidates every time (max-age=0), the edge holds it for five minutes, serves a stale copy for a further minute while it refreshes, and keeps serving that copy for a day if your origin falls over. Pair this with purge-on-publish (step 7) and five minutes never means five minutes of wrong content.
4. Classes 3 and 4 — bypass, on path and on cookie. These are the safety rules, so they are evaluated first. Walk any request down this tree and it lands on exactly one action.
The path bypass list for a WooCommerce store:
/wp-admin/*
/wp-login.php
/wp-cron.php
/cart/
/checkout/
/my-account/
/*?wc-ajax=*
/*?add-to-cart=*
The wc-ajax line is the one people miss. WooCommerce refreshes the mini-cart through ?wc-ajax=get_refreshed_fragments, and a cached response there shows every visitor the same cart contents. Also bypass every request whose method is not GET or HEAD.
5. Bypass on cookie presence, never on cookie value. If any of these cookies is present, skip the cache entirely:
wordpress_logged_in_*
wp-postpass_*
comment_author_*
woocommerce_items_in_cart
woocommerce_cart_hash
wp_woocommerce_session_*
Match on the prefix, because WordPress appends a hash to wordpress_logged_in_ and WooCommerce appends one to wp_woocommerce_session_. Match on presence only: putting the cookie value into the cache key gives you one entry per user, which is a cache that never hits plus a storage bill. This bypass is also why the fifth rule below is safe — anonymous visitors carry none of these.
6. Normalize the key. Tracking parameters change nothing about the response, so drop them; sort what remains so parameter order stops creating duplicates.
Drop from cache key: utm_* fbclid gclid msclkid mc_eid _ga ref
Keep in cache key: ver s page paged product_cat orderby min_price max_price
Sort retained params alphabetically before hashing.
Without this, one shared campaign link produces a fresh MISS for every recipient. With it, /shop/?utm_source=a and /shop/?utm_source=b share one entry.
7. Fix Vary at origin, then wire up purge. Strip the plugin-generated Vary: Cookie on public HTML and re-emit a correct one:
fastcgi_hide_header Vary;
add_header Vary "Accept-Encoding" always;
Then purge on write rather than on a timer. Purge the single URL on save_post, the archive and taxonomy URLs on transition_post_status, the product and shop URLs on woocommerce_update_product, and reserve a full purge for a deploy that changes every asset. Purge fixes staleness; it does not fix leakage, so never let it stand in for the bypass rules.
Validate it works
Run all five. Each one checks a rule you just wrote.
# 1. A fingerprinted asset: long max-age, and a HIT with a rising Age
curl -sSI 'https://www.example.com/wp-content/themes/mytheme/style.css?ver=1.4.2' \
| grep -iE '^(cache-control|age|cf-cache-status|x-cache)'
# 2. Public HTML for an anonymous visitor: should become a HIT on the second call
curl -sSI https://www.example.com/shop/ | grep -iE '^(cache-control|age|cf-cache-status|x-cache)'
curl -sSI https://www.example.com/shop/ | grep -iE '^(age|cf-cache-status|x-cache)'
# 3. The same URL with a logged-in cookie must NOT be a HIT
curl -sSI https://www.example.com/shop/ -H 'Cookie: wordpress_logged_in_9f2=someuser' \
| grep -iE '^(cache-control|cf-cache-status|x-cache)'
# 4. Tracking parameters must share one entry
curl -sSI 'https://www.example.com/shop/?utm_source=a' | grep -i '^age:'
curl -sSI 'https://www.example.com/shop/?utm_source=b' | grep -i '^age:'
# 5. Cart fragments must never be cached
curl -sSI 'https://www.example.com/?wc-ajax=get_refreshed_fragments' \
| grep -iE '^(cache-control|cf-cache-status|x-cache)'
The success signals, in order: check 1 shows max-age=31536000 and a non-zero age; check 2 shows MISS then HIT; check 3 shows a bypass status, never HIT; check 4 shows both calls reporting the same climbing age, which proves they hit one entry; check 5 shows a bypass status and no-store or private.
Then look at the ratio, not the anecdote. Read hit ratio from the CDN’s analytics and track assets and HTML separately — assets dominate request counts and will happily hide an HTML cache that never warms. On a WooCommerce store with these rules, overall request hit ratio usually sits high because of the assets, while HTML sits far lower and is the number worth watching week to week.
Troubleshooting
Every HTML response is a MISS. The origin is sending Set-Cookie on every response — a PHP session_start(), a consent plugin, an A/B tester — and every correct shared cache treats a response with Set-Cookie as private. Confirm with curl -sSI https://www.example.com/ | grep -i set-cookie, then fix it at origin. Do not blanket-strip Set-Cookie at the edge: too broad a rule breaks login.
A logged-in visitor sees an anonymous page, or someone else’s cart. The bypass rule did not fire. In order of likelihood: the bypass rule is ordered after the HTML cache rule and first match wins; the cookie match is exact instead of a prefix, so the hash suffix defeats it; or the page was already stored before you added the rule, and you never purged.
Hit ratio dropped off a cliff after a change. Something joined the cache key. Check the response Vary first, then the CDN’s cache-key configuration, then any new cookie a plugin started setting. Multiply the distinct values of every keyed element together — that product is how many MISSes you now need per URL before the cache is useful again.
Age stays at 0 but the status header says HIT. Common with tiered caching: the header can describe the tier that answered while a different tier holds the copy. Not necessarily a fault. Trust the analytics hit ratio over a single header on a single request.
A purge appears to do nothing. You purged a key other than the one being served. Usually it is a host mismatch (example.com versus www.example.com), a scheme mismatch, or a purge-by-URL that dropped a query string you deliberately kept in the key.
When this is the wrong approach
- Every page is genuinely per-user. A logged-in dashboard has no public HTML to cache. Cache the assets, cache the handful of API responses that are identical for everyone, and put the remaining effort into origin-side object caching and a fast database instead.
- Very low traffic. The asset class still pays for itself, but a five-minute HTML TTL on a site with a few hundred visits a day mostly produces a cache that is always cold. Set the asset rules and stop.
- You are standardised on one vendor. Write that vendor’s version of this matrix rather than the neutral one — the Cloudflare-specific rule syntax, cache statuses and edge cases are in Cloudflare cache rules and cacheability.
- Data residency constraints. An edge cache means copies of the response land in every PoP that serves a request for it. If a contract or a regulation says where the data may rest, that content does not belong in a global shared cache.
The normative definitions of Cache-Control, Vary, Age and freshness are in RFC 9111, HTTP Caching, and MDN’s HTTP reference is the readable version of the same rules.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.