Caching full WordPress HTML pages on a CDN’s edge servers makes pages load much faster for distant visitors, but only if logged-in users, carts and forms are kept out of the cache. Without bypass rules, admins see guest pages, shoppers see someone else’s WooCommerce cart, and form nonces fail. This guide shows how to configure CDN edge cache rules for WordPress cookies and dynamic URLs, back them up with Nginx headers, and prove they work with curl.
Quick answer: Cache anonymous HTML at the edge, but send any request with a WordPress login, comment or WooCommerce cookie, or an admin, cart or checkout URL, straight to the origin.
- List the cookies that must skip the cache:
wordpress_logged_in_,comment_author_,wp-postpass_and the WooCommerce cart and session cookies. - Make Nginx send
Cache-Control: no-storefor those cookies, admin paths and any response that sets a cookie. - In the CDN, put bypass rules for paths, cookies and preview or cart query strings above a final “cache everything” rule.
- Test with
curl -I: a guest request should HIT, a logged-in cookie or/wp-login.phpshould BYPASS.
In this guide
- Before You Start
- Why Edge Caching Breaks WordPress
- Step 1: Identify the WordPress Cookies That Must Bypass the Cache
- Step 2: Send Cache Signals from Nginx
- Step 3: Create CDN Edge Rules for Paths, Cookies and Query Strings
- Step 4: Handle the REST API and Query Strings in an Edge Worker
- Step 5: Check the Bypass Rules with curl
- If the Tests Fail
- Watch Out for Cached Set-Cookie Headers
- WordPress Edge Caching FAQ
- Next Steps

Before You Start
- Access: SSH with
sudoon the Nginx server that runs WordPress (a VPS or dedicated server, not shared hosting), plus admin access to your CDN’s cache rules. - Time: about 30–60 minutes, including testing.
- Backup: copy your Nginx site file before editing, for example
sudo cp /etc/nginx/sites-available/yourdomain.com ~/yourdomain.com.bak. - Risk: a wrong rule can show one visitor’s cart or account page to another. Test on a staging domain or at a quiet time, and keep the CDN’s “purge everything” button handy.
Why Edge Caching Breaks WordPress
CDNs handle static files like CSS, JavaScript and images easily because they are identical for everyone. HTML is different. WordPress builds each page for the person asking: an admin who opens the homepage gets the admin toolbar in the markup.
If your CDN caches that response and serves it to a guest, the guest sees the admin bar. Worse, a page built for a shopper with items in the cart can show that cart to the next visitor.
To keep the cache clean while serving most anonymous traffic from the edge, you need three layers of bypass logic:
- Dynamic URLs: paths like
/wp-admin/,/wp-login.php,/cart/and/checkout/must never be stored. - WordPress cookies: any request with a login or session cookie has to skip the edge cache.
- Origin Cache-Control headers: the origin server tells the CDN when a response is private or short-lived, as a safety net if a CDN rule misfires.
Step 1: Identify the WordPress Cookies That Must Bypass the Cache
WordPress and WooCommerce use fixed cookie name patterns, listed in the official WordPress cookie documentation. Your edge rules check the incoming Cookie header against these patterns:
wordpress_logged_in_.*: set when a user logs in. They need live dashboards and edit buttons.comment_author_.*: set after someone comments, so their details prefill. Caching it leaks commenter names to strangers.wp-postpass_.*: set when a visitor enters the password for a password-protected post.woocommerce_items_in_cart: set when a shopper adds an item to the cart.woocommerce_cart_hash: tracks cart contents.wp_woocommerce_session_.*: holds the shopper’s session, including guest checkout.
If any of these cookies is present, the CDN must send the request to your origin server without using or storing a cached copy.
Step 2: Send Cache Signals from Nginx
Your origin should tell the CDN what it may cache. Even with rules in the CDN dashboard, sending the right Cache-Control directives from Nginx stops accidental caching if a rule misfires.
On the server, open your Nginx site file (usually in /etc/nginx/sites-available/):
sudo nano /etc/nginx/sites-available/yourdomain.comAdd the four map blocks at the top of the file, outside the server block. Sites files are loaded inside Nginx’s http block, so that is the right level. Then add the two cache lines to your existing PHP location block; the server block below shows where they go, so do not paste a second copy of it:
# --- http {} context (outside the server block) ---
# Detect WordPress auth or cart cookies
map $http_cookie $skip_cache_cookie {
default 0;
~*wordpress_logged_in_ 1;
~*comment_author_ 1;
~*wp-postpass_ 1;
~*woocommerce_items_in_cart 1;
~*woocommerce_cart_hash 1;
~*wp_woocommerce_session_ 1;
}
# Detect paths that must never be cached
map $request_uri $skip_cache_uri {
default 0;
"~*/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml" 1;
}
# Treat the response as dynamic if PHP sets a cookie or sends its own Cache-Control
map "$upstream_http_set_cookie$upstream_http_cache_control" $origin_dynamic {
default 1;
"" 0;
}
# Pick the Cache-Control value sent to the CDN
map "$skip_cache_cookie$skip_cache_uri$origin_dynamic" $edge_cache_control {
default "no-store, no-cache, must-revalidate, max-age=0";
"000" "public, max-age=300, stale-while-revalidate=60";
}
# --- server {} context ---
server {
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
# Replace PHP's own Cache-Control header with the edge signal
fastcgi_hide_header Cache-Control;
add_header Cache-Control $edge_cache_control always;
}
}This sends Cache-Control: no-store whenever a logged-in user or active shopper loads a PHP page, whenever the URL is on the bypass list, and whenever PHP sets a cookie or sends its own Cache-Control header. Everything else is marked public for 5 minutes (max-age=300), with stale-while-revalidate letting the CDN serve a slightly old copy while it fetches a fresh one.
Test the configuration and reload Nginx:
sudo nginx -t && sudo systemctl reload nginxYou should see syntax is ok and test is successful. If nginx -t reports an error, fix it before reloading; the running server keeps the old configuration until the test passes.
Step 3: Create CDN Edge Rules for Paths, Cookies and Query Strings
Now set up the rules in your CDN. Order matters: put the bypass rules at the top so they run before the final “cache everything” rule. The labels below are generic; each CDN names these options slightly differently.
Rule 1: Bypass Dynamic Paths
Condition: the URL path matches any of:
/wp-admin/*/wp-login.php/wp-signup.php/cart/*/checkout/*/my-account/*/wc-api/*
Action: bypass the cache (edge TTL 0, browser TTL 0). TTL, or time to live, is how long a copy may be reused.
Rule 2: Bypass Requests with WordPress Cookies
Condition: the Cookie header matches this regular expression:
(wordpress_logged_in_|comment_author_|wp-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wp_woocommerce_session_)Action: bypass the cache and forward all request headers to the origin.
Rule 3: Bypass Preview and Cart Query Strings
WordPress and WooCommerce use these query strings for previews, the Customizer and cart actions. They must never be cached:
(preview=true|wp_customize=|customize_changeset_uuid|wc-ajax=|add-to-cart=)Action: bypass the cache. Marketing parameters such as utm_source are a different case: you usually want to strip them so they do not split the cache. See how to strip marketing query strings at the CDN edge.
Rule 4: Cache Everything Else
Condition: the URL path matches * (all remaining requests).
Action: cache HTML and respect the origin’s Cache-Control headers. You can set a longer edge TTL instead if your site purges the CDN cache on publish. Turn on stale-while-revalidate if your CDN supports it, to soften traffic spikes. When popular pages expire under heavy load, also look at how to prevent CDN cache stampedes with origin shield.
Step 4: Handle the REST API and Query Strings in an Edge Worker
WordPress REST API endpoints (/wp-json/*) serve both public data and authenticated requests. Apps and headless front ends often log in with an Authorization header instead of a cookie, so the edge must check that header too.
If your CDN runs code at the edge, you can put all the bypass logic in one script. This example uses the Cloudflare Workers service-worker syntax; adapt it to your platform:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const cookieHeader = request.headers.get('Cookie') || '';
const authHeader = request.headers.get('Authorization') || '';
// Cookies that must bypass the cache
const bypassCookiePattern = /(wordpress_logged_in_|comment_author_|wp-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wp_woocommerce_session_)/;
// Check if the request requires dynamic origin processing
const isBypassCookie = bypassCookiePattern.test(cookieHeader);
const isAuthRequest = authHeader.length > 0;
const isProtectedPath = new RegExp('^/(wp-admin|wp-login.php|cart|checkout|my-account)').test(url.pathname);
const isDynamicQuery = url.searchParams.has('preview') || url.searchParams.has('wc-ajax');
if (isBypassCookie || isAuthRequest || isProtectedPath || isDynamicQuery) {
// Send the request straight to the origin and skip the edge cache
return fetch(request, { cache: 'no-store' });
}
// Fall back to the standard edge cache pipeline
return fetch(request);
}The worker runs before the cache lookup. On Cloudflare Workers, the cache: 'no-store' fetch option needs a recent compatibility date; older Workers reject it. With this in place, a request to /wp-json/wp/v2/posts that carries a JWT or Bearer token skips the cached public copy and gets the user’s own view.
Step 5: Check the Bypass Rules with curl
Test from your local terminal with curl -I, which prints only the response headers. Browser dev tools are less reliable here, because the browser’s own cache and service workers can hide what the CDN did. The name of the cache status header depends on your CDN: often x-cache, or cf-cache-status on Cloudflare.
Test 1: An Anonymous Visitor Should Get a Cache HIT
curl -I https://example.com/sample-post/Run it twice; the first request is often a MISS while the CDN stores the page. The second should look similar to this:
HTTP/2 200
content-type: text/html; charset=UTF-8
x-cache: HIT
age: 142
cache-control: public, max-age=300, stale-while-revalidate=60HIT and a non-zero age (seconds since the copy was stored) confirm the CDN served the page from its cache.
Test 2: A Logged-In Cookie Should BYPASS the Cache
curl -I -H "Cookie: wordpress_logged_in_testcookie=1" https://example.com/sample-post/Expected result:
HTTP/2 200
content-type: text/html; charset=UTF-8
x-cache: BYPASS
age: 0
cache-control: no-store, no-cache, must-revalidate, max-age=0BYPASS and age: 0 show that your rule matched the cookie and sent the request to Nginx. Cloudflare may report DYNAMIC instead, which also means the page was not served from cache.
Test 3: The Login Page Should BYPASS the Cache
curl -I https://example.com/wp-login.phpExpected result:
HTTP/2 200
content-type: text/html; charset=UTF-8
x-cache: BYPASS
cache-control: no-store, no-cache, must-revalidate, max-age=0If the Tests Fail
- Test 2 or 3 returns HIT. Check the rule order (bypass rules must come first) and look for typos in the cookie pattern. Then purge the CDN cache, because a page cached before the fix stays cached until it expires.
- Every request returns MISS. Check whether your origin sends
Cache-Control: privateorPragma: no-cache. Some security plugins add these to every page. Turn that off for public pages; the Nginx config above treats anyCache-Controlheader from PHP as “do not cache”. nginx -tsays"map" directive is not allowed here. Amapblock ended up inside theserverblock. Move it above theserverline.- Security headers disappeared from PHP pages. In Nginx, an
add_headerinside alocationreplaces everyadd_headerset at theserverlevel. Repeat headers such as HSTS inside the PHPlocationblock. - The site goes into a redirect loop. That is usually an SSL mode mismatch between CDN and origin, not a cache rule. See how to fix ERR_TOO_MANY_REDIRECTS on Nginx behind a CDN.
Watch Out for Cached Set-Cookie Headers
HTML edge caching has one dangerous failure: anonymous visitors receiving session cookies that belonged to someone else.
It happens when the origin sends a Set-Cookie header on a response the CDN treats as cacheable. The CDN may store the header along with the HTML, and every guest who gets the cached page then receives that session cookie.
The rule is simple: never mark a response as cacheable when it sets a cookie. The Nginx config from Step 2 already does this. The $origin_dynamic map switches the response to no-store whenever PHP sends a Set-Cookie header:
# Treat the response as dynamic if PHP sets a cookie or sends its own Cache-Control
map "$upstream_http_set_cookie$upstream_http_cache_control" $origin_dynamic {
default 1;
"" 0;
}At the edge, check how your CDN handles Set-Cookie on cacheable responses; the setting names differ between providers. Whatever it is called, a response that sets a cookie must pass through uncached.
WordPress Edge Caching FAQ
Will caching HTML at the edge break my contact forms and nonces?
Not with a short TTL. WordPress nonces (one-time security tokens in forms) stay valid for 12 to 24 hours by default, per the WordPress developer documentation. An edge TTL of 2 hours or less is safe. For 24-hour TTLs, submit forms through REST endpoints or refresh nonces with AJAX on page load.
Can I use WooCommerce and edge caching together?
Yes. The rules above stop caching as soon as an item goes into the cart, because WooCommerce then sets the woocommerce_items_in_cart cookie. Product pages and archives stay fast for anonymous browsers, while carts, checkouts and active sessions always reach PHP.
Should I cache the WordPress REST API at the edge?
Only public read endpoints, and only briefly. You can cache requests like GET /wp-json/wp/v2/posts for 60 to 300 seconds if the cache key includes the query string. Any request with an Authorization header or a login cookie must bypass the cache entirely.
Do I still need a caching plugin with CDN edge caching?
Usually yes, for purging. Without a plugin or CDN integration that clears the edge cache when you publish or update a post, visitors keep seeing the old page until the TTL runs out. Many CDNs offer an official WordPress plugin for this.
Next Steps
Once the bypass rules pass all three tests, watch your origin’s CPU and PHP worker usage for a day; cached anonymous traffic should no longer reach PHP or MySQL. Then cache static files for longer with immutable Cache-Control and cache busting in Nginx, so browsers stop re-checking CSS and JavaScript on every visit.

