Skip to content

Cookies and Cloudflare cache bypass: which cookies skip the cache, and which you must handle yourself

Cloudflare does not bypass its cache just because a request carries cookies — but it refuses to store responses that set them. Here is the actual cookie behaviour, the session and cart cookies worth bypassing on, and the rule that does it on any plan.

Two cookie rules govern Cloudflare’s cache, and teams routinely assume the opposite of both. One: cookies in the request do not bypass the cache by default — a logged-in user asking for a cached page gets the cached page. Two: a Set-Cookie header in the response prevents caching by default — unless an Edge TTL override caches the response anyway and strips the header. Everything session-aware you want (logged-in users skipping cache, carts staying live) you must build yourself with a bypass rule on named cookies.

What’s happening

The layers: browser cache → Cloudflare edge → origin caches. Out of the box, Cloudflare edge-caches static file extensions only, never HTML, so cookie behaviour barely matters — assets are the same for everyone. The moment you cache HTML (Cache Everything, a Cache Rule, APO), cookies become the line between “fast site” and “serving one customer’s cart to another”.

On the request side, the cache key is host + path + query string. The Cookie header is not in it, and Cloudflare does not vary or bypass on it unless a rule says so. On the response side, Set-Cookie marks the response as per-visitor and the default behaviour is to not store it. The exception is deliberate: a Cache Rule Edge TTL set to “Ignore cache-control header and use this TTL” (or the legacy Edge Cache TTL page rule) stores the response and drops the Set-Cookie — useful for origins that pointlessly set cookies on static files, dangerous on pages that were issuing a session.

APO handles WordPress automatically, bypassing on the logged-in, comment-author, and WooCommerce session and cart cookie families. Without APO, that list is yours to implement — Cache Rules expressions can match http.cookie on every plan, which replaced the old Business-plan “Bypass Cache on Cookie” page-rule setting for most uses.

What to check first

  • Whether HTML is cached at all. cf-cache-status: DYNAMIC on pages means no cookie rule is needed yet — and none of this is your current problem.
  • Which cookies your platform issues. WordPress: wordpress_logged_in_*, comment_author_*. WooCommerce: woocommerce_cart_hash, woocommerce_items_in_cart, wp_woocommerce_session_*. Generic PHP: PHPSESSID. Laravel: laravel_session.
  • Which cookies exist on every visitor. Analytics cookies (_ga, _gid, _fbp) ride on nearly all requests — any rule that bypasses on “cookie exists” bypasses everything, always.
  • Whether the origin sets cookies on anonymous pages. A session_start() on every page view makes every response carry Set-Cookie, which blocks caching before rules even apply.

How to verify

Compare the same page with and without a session cookie:

curl -sI https://example.com/shop/ | grep -iE '^(cf-cache-status|age|set-cookie)'
curl -sI -H "Cookie: wordpress_logged_in_abc=1" https://example.com/shop/ | grep -iE '^(cf-cache-status|age)'

Target state: first request HIT, second BYPASS. If both read HIT, logged-in traffic is being served the anonymous cache (or worse, populated it). Check what the origin sets on a plain anonymous view, bypassing the edge:

curl -sI "https://example.com/shop/?probe=$(date +%s)" --resolve example.com:443:203.0.113.10 | grep -i '^set-cookie'

Any Set-Cookie here is what keeps the page out of cache by default — decide whether it belongs on anonymous traffic at all.

The fix

  1. Name the cookies that mean “this visitor is special”. Session, login, and cart cookies only. Analytics and consent cookies stay off the list.

  2. Write the bypass rule. One Cache Rule, action “Bypass cache”:

    (http.cookie contains "wordpress_logged_in") or
    (http.cookie contains "wp_woocommerce_session") or
    (http.cookie contains "woocommerce_items_in_cart") or
    (http.cookie contains "PHPSESSID")
  3. Order it to win. In Cache Rules, later rules override earlier ones when both match — place the bypass rule after your HTML-caching rule. Plan note: this costs one rule out of 10 on Free, 25 on Pro, 50 on Business, 125 on Enterprise.

  4. Clean up origin Set-Cookie behaviour. Stop issuing sessions to anonymous visitors; start the session at login or add-to-cart instead. That keeps anonymous responses cookie-free and cacheable without edge tricks.

  5. Use Set-Cookie stripping only on static files. An Edge TTL override that strips Set-Cookie is fine for /assets/; on HTML it caches a page that was mid-session-handshake.

  6. Test the sequence. Anonymous (HIT), cookied (BYPASS), anonymous again (HIT, same age still counting) — the third request proves the cookied request neither used nor replaced the shared copy.

When this fix is wrong

If personalization happens client-side — JavaScript reads the cart count from a cookie or localStorage and renders it after load — the HTML is identical for everyone and a bypass rule only sacrifices hit rate. If you need per-user cached pages rather than uncached ones, that is Enterprise custom-key territory and usually a design smell. And if the leak you are chasing is already happening, work the incident response in Cache Everything serving logged-in pages first.

Common mistakes

  • Bypassing when any cookie is present, then wondering why _ga turned the whole zone into BYPASS.
  • Trusting Vary: Cookie — Cloudflare does not use it in the cache key.
  • Matching cookie values instead of names, breaking when the session ID rotates.
  • Forgetting WooCommerce’s cart cookies exist for guests who never log in.
  • Assuming Set-Cookie protection still applies after someone set an Edge TTL override that strips it.

Prevention

Keep the platform cookie list versioned next to your rules and reuse it across zones. Watch the BYPASS share in cache analytics — a sudden climb means a new cookie appeared on anonymous traffic, often from a freshly installed plugin. Review origin Set-Cookie on anonymous paths quarterly; the Cloudflare cache documentation is the reference for cookie interaction defaults, and Cache Rules cacheability covers the eligibility side.

Cookies are the contract between personalization and caching — write it explicitly or it gets written for you. Cache layers that fight each other cost real revenue — Stack Harbor tunes the whole stack, edge to origin, under Managed Cloud Ops; book an infrastructure review.