Bypass CDN Cache for Logged-In WordPress Users in StackPath

by Fahim

Caching WordPress HTML at the edge drops global TTFB from 520ms to under 30ms. It also breaks your site instantly the second an admin edits a post and the CDN serves their floating admin bar to every visitor on the internet. Here is how to configure StackPath EdgeRules to inspect request cookies and URL paths so logged-in users and WooCommerce shoppers bypass edge caching entirely while public visitors still get sub-30ms cache hits.

Bypass CDN Cache for Logged-In WordPress Users in StackPath
Bypass CDN Cache for Logged-In WordPress Users in StackPath

Why Edge Caching Breaks Logged-In WordPress Sessions

By default, an edge server treats an incoming GET / the exact same whether it comes from a visitor in Tokyo or an admin editing site settings in London. Without explicit cookie checks, whoever requests an uncached page first defines what gets saved to disk at the edge POP.

If you hit a post while logged in, your origin generates HTML with your admin bar, edit links, and session-specific nonces. The CDN stores that markup and happily dumps your administrative interface onto the next 50,000 anonymous visitors.

The reverse failure is just as annoying: an anonymous user hits the page first, the edge caches it, and when an editor logs in to check their draft, they see the old cached public page. Edits don’t show up, comment nonces break, and cart totals stay frozen at zero.

To fix this, the edge has to inspect the Cookie header before touching cache storage. If an auth or cart cookie is present, the edge must skip its cache, proxy straight to the origin, and pass the response back untouched. This works right alongside other origin-hardening steps, like when you restrict origin server access to StackPath Origin Shield IPs in Nginx.

The WordPress Cookies That Require Cache Bypassing

WordPress doesn’t use a single static cookie name for authentication. It appends a cryptographic hash derived from your site URL and salt keys, which means exact string matching won’t work. You need to match cookie prefixes with regex.

Open browser devtools on your site and check the Application/Storage tab. Depending on your active plugins, you’ll see a mix of these:

  • wordpress_logged_in_[hash]: Set once a user logs in. If this cookie exists, an active session is live.
  • wordpress_sec_[hash]: Present whenever HTTPS is enforced for auth sessions.
  • comment_author_[hash]: Created when a guest leaves a comment and checks the box to save their details. Caching this breaks form pre-population.
  • woocommerce_items_in_cart: Set by WooCommerce the moment a customer drops an item in their basket.
  • woocommerce_cart_hash: Tracks the current cart state and line items.
  • wp_woocommerce_session_[hash]: Holds session tokens for store visitors.

For specifics on how core handles these, see the WordPress developer documentation on cookies. Knowing these prefixes lets us craft concise regex rules that catch every session variation.

Step 1: Create the Authentication Cookie Bypass Rule

StackPath evaluates request routing logic through EdgeRules. These evaluate incoming requests against conditions you define, then apply actions like altering cache behavior or tweaking headers.

In the StackPath Control Portal, open your CDN site and head to EdgeRules. Click Add EdgeRule:

  1. Rule Name: WordPress: Bypass Logged-In Users.
  2. If (Conditions): Select Request Header.
  3. Header: Cookie.
  4. Operator: Matches (Regex).
  5. Value: Paste the auth pattern below:
(wordpress_logged_in_|comment_author_|wordpress_sec_)

Scroll down to the actions section:

  1. Then (Actions): Select CDN Caching Mode.
  2. Setting: Choose Bypass Cache.
  3. Hit Save.

With this rule saved, any request containing a wordpress_logged_in_ or comment_author_ cookie skips edge storage completely. The POP hands the request to your origin, waits for PHP to render, and sends the raw response back to the user without writing it to disk.

Step 2: Add WooCommerce Cart and Session Protections

An auth rule alone will not protect guest shoppers. When a new customer adds a product to their cart, they don’t have a wordpress_logged_in_ cookie yet. If the edge caches that cart page or checkout fragment, their cart will either look empty on the next page load or show someone else’s items.

I recommend keeping WooCommerce in a separate EdgeRule rather than bundling it into the auth rule. It makes debugging much cleaner when auditing store traffic.

Create a second rule named WooCommerce: Bypass Cart & Checkout Sessions:

  1. If (Conditions): Request Header -> Cookie.
  2. Operator: Matches (Regex).
  3. Value: Use this pattern:
(woocommerce_items_in_cart|woocommerce_cart_hash|wp_woocommerce_session_)
  1. Then (Actions): CDN Caching Mode -> Bypass Cache.
  2. Hit Save.

Guest visitors get instant cache hits across your product catalog right until they click “Add to Cart”. Once WooCommerce drops the woocommerce_items_in_cart cookie, all subsequent requests from that visitor hit PHP directly so prices, taxes, and cart counts stay accurate.

Step 3: Exempt Sensitive URL Paths at the Edge

Relying only on cookies leaves a blind spot: the login page itself. When you browse to /wp-login.php, you don’t have an auth cookie yet. If the CDN caches that response, you’ll run into stale login nonces and bizarre redirect loops.

You need a path-based rule to back up your cookie checks. Create a third EdgeRule named WordPress: Path Exemptions:

  1. If (Conditions): Select URL Path.
  2. Operator: Matches (Regex).
  3. Value: Match these critical paths:
^/(wp-admin/|wp-login.php|xmlrpc.php|cart|checkout|my-account)
  1. Then (Actions): CDN Caching Mode -> Bypass Cache.
  2. Hit Save.

This guarantees that admin URLs, AJAX endpoints, and WooCommerce checkout screens never touch edge cache regardless of what cookies the client sends. If you are tuning your upstream architecture during high-traffic events, see our guide on how to configure Origin Shield in StackPath CDN for traffic spikes.

Step 4: Configure Origin Cache-Control Headers in Nginx

EdgeRules handle the routing, but your origin should back them up. If an aggressive WordPress caching plugin sends Cache-Control: public, max-age=86400 on an admin page, edge servers can get confused depending on rule priority.

Add a map block in your Nginx config to force Cache-Control: private, no-cache whenever WordPress auth cookies are detected.

Open your Nginx virtual host file:

sudo nano /etc/nginx/sites-available/your-site.conf

Add this mapping logic inside the server {} block:

# Detect logged-in WordPress or WooCommerce users
map $http_cookie $bypass_cache { default 0; "~*wordpress_logged_in_" 1; "~*comment_author_" 1; "~*woocommerce_items_in_cart" 1;
} server { # ... existing site configuration ... location ~ .php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Prevent downstream proxies from caching authenticated sessions if ($bypass_cache = 1) { add_header Cache-Control "private, no-cache, no-store, must-revalidate" always; } }
}

Test the configuration and reload Nginx:

sudo nginx -t && sudo systemctl reload nginx

Now, whenever an authenticated request hits your origin, Nginx explicitly tells both the browser and CDN not to cache the response under any circumstances. Review the MDN documentation on HTTP caching if you want a refresher on how upstream proxies handle no-store and private directives.

Testing Cache Bypass with cURL

Don’t rely on your browser to verify edge caching. Between local disk caches, service workers, and prefetching, browsers lie to you. Use curl to see the actual headers coming off the wire.

First, test an unauthenticated request to a public page:

curl -I https://example.com/sample-post/

Check the caching headers in the response:

HTTP/2 200
content-type: text/html; charset=UTF-8
x-cache: HIT
age: 142
cache-control: public, max-age=86400

The x-cache: HIT confirms the edge POP served the response straight from disk in about 18ms without pinging your origin.

Now send the same request with a dummy WordPress login cookie attached using -H:

curl -I -H "Cookie: wordpress_logged_in_abcdef123456=admin" https://example.com/sample-post/

Check the headers again:

HTTP/2 200
content-type: text/html; charset=UTF-8
x-cache: BYPASS
cache-control: private, no-cache, no-store, must-revalidate

The header shows x-cache: BYPASS (or MISS depending on edge configuration). The Age header is gone because the POP proxied the request to your Nginx backend, waited for PHP-FPM, and streamed the result back. If you still see x-cache: HIT here, your regex pattern didn’t match or another rule higher up caught the request first.

Gotchas and Common EdgeRule Pitfalls

I’ve run into a few edge cases setting this up on client sites. Watch out for these three:

1. Rule Execution Order

StackPath evaluates EdgeRules from top to bottom. If you have a catch-all caching rule sitting at position 1 (like “Cache everything under /*“), it executes before your cookie checks. Always drag your bypass and path-exemption rules to the very top of your EdgeRules list.

2. The Admin Bar Still Appears for Logged-Out Users

If you turned on edge caching before setting up bypass rules, an admin session might already be cached on POP storage. Saving an EdgeRule does not clear old cache objects. You have to purge:

  1. Open your site in the StackPath portal.
  2. Click Purge Cache.
  3. Select Purge Everything and confirm.

Give it 60 seconds to clear across all edge datacenters before running your curl checks again.

3. Cookie Stripping Plugins

Some WordPress performance plugins strip cookies to make static asset caching more aggressive. If a plugin strips cookies before PHP inspects them, users get logged out randomly and sessions break. Make sure cookie stripping is restricted to static asset directories like /wp-content/uploads/, never on HTML document requests.

If static assets act up after modifying edge headers, check your CORS configuration; read our guide on how to fix CORS errors on web fonts and static assets in StackPath CDN.

Frequently Asked Questions

Can I bypass the cache using a query parameter instead of cookies?

Yes. Add a condition in StackPath EdgeRules for URL Query String matching something like nocache=true. It’s handy for debugging or checking draft layouts when you don’t have access to your normal browser cookies.

Will bypassing cache for logged-in users slow down my origin server?

Only for authenticated sessions. Public traffic—which is usually 90% to 95% of your volume—still hits the edge cache. Only logged-in authors, admins, and active cart shoppers will generate PHP-FPM processes on your origin.

Does this replace local caching plugins like WP Rocket or W3 Total Cache?

No. Keep your caching plugin active for object caching, transients, and database query optimization. Just make sure its own page caching is disabled for logged-in users so origin responses stay dynamic when the CDN passes through.

How do I prevent cache stampedes when purging dynamic pages?

When you publish a post and purge the CDN, a rush of concurrent requests can slam your origin PHP workers simultaneously. To smooth those spikes out, see our walkthrough on how to prevent CDN cache stampedes with Origin Shield and stale-while-revalidate.

Next Steps for Your CDN Setup

Once your EdgeRules are running and verified with curl, keep an eye on your CDN analytics over the next 24 hours. Your cache hit ratio should settle above 85% on content sites while admin workflows and checkouts run clean. Next, lock down your origin firewall to drop any direct requests that don’t pass through your edge network.

all_in_one_marketing_tool