A reliable caching setup is less about choosing one fast layer than making every layer agree on freshness, bypass rules, and invalidation. This cache configuration matrix brings together origin headers, Nginx FastCGI cache, Varnish, Cloudflare, and other CDNs, then turns them into a deployment checklist you can use before a launch, migration, plugin update, or traffic spike.
Overview
Most production websites have several caching layers: an application or page cache, an origin web server, a reverse proxy, and a CDN or edge cache. A separate object cache such as Redis may also reduce database work. Each layer can improve performance, but each can also preserve outdated content or bypass a more useful cache when its rules are unclear.
The safest design gives every content type a deliberate policy. Public HTML can normally be cached for a short period if it has a dependable purge path. Versioned CSS, JavaScript, fonts, and images can use longer browser and edge lifetimes. Account pages, carts, checkout screens, administrator paths, and responses containing private data should usually bypass shared caches.
Use the following matrix as a starting point, not as a universal policy. Confirm behavior in staging and adjust TTLs to match how frequently content changes and how quickly your team can purge it.
| Content or layer | Suggested policy | Typical starting point | Validation |
|---|---|---|---|
| Public HTML | Shared-cacheable when anonymous and safe | Browser: 0–300 seconds; edge: 60–600 seconds | Check Age, cache status, and purge behavior |
| Static assets | Use immutable, versioned filenames | Browser and edge: 30 days to 1 year | Change the filename when content changes |
| Authenticated or private HTML | Do not store in shared caches | private, no-store where appropriate | Test with two different users |
| APIs | Cache only explicitly public, idempotent responses | 0–60 seconds unless documented otherwise | Verify method, authorization, query-string, and cookie handling |
| Origin page cache | Cache anonymous GET and HEAD requests | 60–600 seconds | Test logged-in, preview, cookie, and error paths |
| Object cache | Cache database results, not complete private pages | Application-defined expiration | Measure hit rate and invalidation after writes |
For a deeper explanation of browser and shared-cache directives, see the Cache-Control and TTL guide. If you are comparing page cache and Redis object cache, use the server cache comparison to separate their roles rather than treating them as interchangeable.
Checklist by scenario
1. Establish the header contract at the application
Set response headers before configuring a CDN or reverse proxy. A public response might use:
Cache-Control: public, max-age=60, s-maxage=300, stale-while-revalidate=30
Surrogate-Control: max-age=600Here, max-age guides browsers, s-maxage gives shared caches a separate lifetime, and Surrogate-Control can provide an edge-specific instruction where supported. For content that must not enter a shared cache, use a policy such as Cache-Control: private, no-store, based on the application’s privacy requirements. Do not add public caching to a response merely because it is fast to generate.
2. Configure Nginx FastCGI cache
Keep the cache key predictable and bypass requests that may contain a session or personalized output. A simplified pattern is:
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:100m inactive=60m use_temp_path=off;
fastcgi_cache_key '$scheme$request_method$host$request_uri';
map $request_method $skip_method {
default 1;
GET 0;
HEAD 0;
}
map $http_cookie $skip_cookie {
default 0;
~*'comment_author|wordpress_logged_in|woocommerce_items_in_cart|wp-postpass' 1;
}
location ~ \.php$ {
fastcgi_cache WORDPRESS;
fastcgi_cache_methods GET HEAD;
fastcgi_cache_bypass $skip_method $skip_cookie;
fastcgi_no_cache $skip_method $skip_cookie;
fastcgi_cache_valid 200 5m;
fastcgi_cache_valid 404 1m;
add_header X-Origin-Cache $upstream_cache_status always;
}This is an illustrative pattern, not a drop-in WordPress configuration. Check the syntax against your Nginx version and application. Add explicit bypasses for administrator paths, previews, checkout, account pages, and any cookie your application uses for personalization. Provide a purge mechanism, whether that is a controlled file purge, an application hook, or a deployment step.
3. Configure Varnish
In VCL, separate lookup policy from storage policy. The exact syntax depends on your Varnish version and deployment, but the logic should resemble:
sub vcl_recv {
if (req.method != 'GET' && req.method != 'HEAD') {
return (pass);
}
if (req.url ~ '^/wp-admin/' || req.url ~ '^/wp-login\.php' || req.http.Authorization) {
return (pass);
}
if (req.http.Cookie ~ 'wordpress_logged_in|woocommerce_items_in_cart') {
return (pass);
}
}
sub vcl_backend_response {
if (beresp.status >= 500) {
set beresp.uncacheable = true;
set beresp.ttl = 0s;
return (deliver);
}
set beresp.ttl = 5m;
}Do not cache an error response for the same duration as a successful page. Decide whether stale delivery is acceptable during an origin outage and document that choice. Varnish invalidation should be authenticated and restricted; a broad purge endpoint exposed to the public internet is a deployment risk.
4. Configure Cloudflare or another CDN
Use the provider’s current cache rules or equivalent rules engine to define: what is eligible, which requests bypass cache, the cache key, edge TTL, browser TTL, and purge scope. Product names and dashboard locations change, so record the rule ID, exported configuration, or API request in version control rather than relying on a screenshot. Older “page rule” features may still exist in some accounts, but use the provider’s currently supported mechanism when creating new rules.
A sensible order is:
- Bypass administrator, login, account, cart, checkout, preview, and authenticated requests.
- Cache public static assets and set a long edge TTL only when filenames are versioned.
- Cache anonymous HTML only when the origin sends a compatible policy and the cache key excludes irrelevant tracking parameters.
- Set a narrow purge path for deployments and a broader purge procedure for emergencies.
- Inspect the response headers from an actual edge location and confirm whether the response was a hit, miss, bypass, or revalidation.
For cache-key decisions, including query strings and cookies, refer to the CDN cache keys guide. Never assume that a successful purge request means every edge has already revalidated the object; test the URL and monitor the provider’s purge result.
5. Apply the deployment and rollback checklist
- Export or commit Nginx, VCL, CDN rules, header definitions, and purge scripts.
- List every bypass path and the cookies or authorization signals that trigger it.
- Record the expected cache status for anonymous, logged-in, preview, and failed requests.
- Warm only safe public URLs; do not use a crawler that can submit forms or create sessions.
- Deploy the configuration to staging and test headers, redirects, compression, cookies, and content variants.
- After production deployment, request a representative URL twice from more than one network or edge region.
- Compare origin load, TTFB, error rate, cache-hit ratio, and key user journeys with the pre-deployment baseline.
- If private data, broken assets, unexpected stale content, or elevated errors appear, disable the newest cache rule, purge affected objects, and restore the last known-good configuration.
What to double-check
Cache keys: Confirm whether the key includes scheme, host, path, query parameters, language, device hints, cookies, or authorization. Removing a meaningful variant can serve the wrong page; including every tracking parameter can destroy the hit rate.
Invalidation: Test publishing, editing, deleting, and rolling back content. A WordPress cache plugin, origin cache, reverse proxy, and CDN may each require a separate purge. For WooCommerce, exclude cart, checkout, account, and session-dependent endpoints; the WooCommerce caching checklist covers these boundaries.
Stale behavior: Define whether stale content may be served while the origin is slow or unavailable. If you enable stale serving, pair it with monitoring and a purge path. A stale public article is different from stale inventory, pricing, permissions, or account data.
Headers and observability: Preserve useful diagnostics such as Age, ETag, Last-Modified, and an internal cache-status header where appropriate. Avoid exposing sensitive backend details. Use a header capture tool and an origin-bypass request to distinguish CDN delay from application or hosting delay.
Capacity: Cache configuration is not a substitute for capacity planning. Measure origin CPU, memory, database latency, connection limits, and network saturation during a controlled load test. The TTFB and Core Web Vitals checklist provides a repeatable measurement workflow.
Common mistakes
- Stacking TTLs without a purge plan: A five-minute origin cache behind a one-hour edge cache can leave users with outdated content for longer than expected.
- Caching by URL alone: Cookies, authorization, language, and device-specific variants can make a URL unsafe to share.
- Purging only the application: Clearing a WordPress plugin or Nginx cache does not necessarily remove an object already stored at the CDN.
- Using long TTLs for unversioned assets: Visitors may retain old CSS or JavaScript until the browser lifetime expires. Rename assets or use a reliable versioning strategy.
- Ignoring non-200 responses: A cached redirect, 404, or 500 can look like an application regression. Assign separate policies and test each status.
- Testing only from one location: One request may be an origin miss while another is an edge hit. Test repeated requests and several representative regions.
- Changing production rules without a rollback: Keep the previous configuration, purge command, and owner contact ready before the change begins.
When to revisit
Review this matrix before seasonal campaigns, major launches, and planned traffic spikes. Recheck it after a hosting or CDN migration, a web-server or Varnish upgrade, a WordPress or cache-plugin update, a change to authentication or checkout, or any modification to URL structure and asset versioning.
Make a quarterly review part of normal operations, and review sooner when a provider changes its rules engine, dashboard, API, plan limits, or purge behavior. Do not copy provider-specific instructions indefinitely: verify the current feature name and API documentation, then update the rule export, snippets, and rollback procedure in your repository.
Before the next deployment, assign one person to run this short sequence: capture the baseline, review the matrix, test staging, verify purge permissions, deploy, request hit and miss cases, inspect origin metrics, and record the result. If any expected behavior is unclear, leave the cache disabled for that path until the owner can define its privacy, freshness, and invalidation requirements. That small discipline keeps website caching an observable part of the release process rather than a source of unexplained stale pages.