On the servers I manage, Nginx FastCGI caching is my preferred way to cache WordPress pages for anonymous visitors. Instead of a WordPress plugin generating and serving cached HTML through PHP, Nginx itself stores the finished page and serves it directly on the next request. PHP and the database never wake up.
The result is remarkably fast. Cached pages are served in milliseconds, and the server can handle far more traffic on the same hardware. During traffic spikes, the difference between a site with server-level caching and one without can be the difference between staying online and falling over.
The catch is that FastCGI caching needs careful rules. Cache the wrong thing, and logged-in users see each other’s dashboards, or customers see someone else’s cart. Here’s how to set it up safely.
How FastCGI caching works
When Nginx passes a request to PHP-FPM, it can save the response in a cache on disk, identified by a key based on the URL. The next request for the same URL is answered from the cache, without passing anything to PHP.
You control:
- Where the cache lives and how large it can grow.
- How long responses are cached.
- Which requests skip the cache, such as logged-in users, POST requests and cart pages.
- How the cache is cleared when content changes.
Step 1: Define the cache zone
In the http block, usually in /etc/nginx/nginx.conf or a file in /etc/nginx/conf.d/, define where the cache is stored:
What these mean:
- keys_zone=WORDPRESS:100m creates a shared memory zone named WORDPRESS for cache keys. 100 MB of key space is plenty for most servers.
- inactive=60m removes cached items not requested for 60 minutes.
- max_size=1g caps the cache size on disk.
- fastcgi_cache_key defines what makes a cached page unique: scheme, method, host and full URI including query string.
- fastcgi_cache_use_stale serves an older cached copy if PHP fails or while a fresh copy is being generated. This keeps the site up during short PHP problems.
- fastcgi_cache_lock ensures only one request generates a new cached page at a time, instead of many requests hitting PHP simultaneously when a popular page expires.
If several sites share the server, give each its own zone, or make sure the host is part of the key, as it is above.
Step 2: Decide what to skip
In the site’s server block, set a variable that decides whether to skip the cache. Start with caching allowed, then skip for anything personal or dynamic:
| if ($request_uri ~* "/wp-admin/ | /wp-login.php | /xmlrpc.php | wp-.*.php | /feed/ | sitemap(_index)?.xml") { |
|---|---|---|---|---|---|
| if ($request_uri ~* "/cart/ | /checkout/ | /my-account/ | /basket/ | /wc-api/") { | |
| if ($http_cookie ~* "wordpress_logged_in_ | wp-postpass_ | comment_author_ | woocommerce_items_in_cart | woocommerce_cart_hash | wp_woocommerce_session_") { |
Adjust the WooCommerce paths to match your store’s actual page slugs. If your cart page is at /basket/ or your account page at /account/, use those.
Skipping all URLs with query strings is a cautious default. Some sites cache certain query strings, like pagination or tracking parameters, but that requires more careful key design. Starting conservatively avoids surprises.
Why cookies matter
WordPress sets a wordpress_logged_in cookie for logged-in users. WooCommerce sets cookies when a customer adds something to their cart. Skipping the cache when these cookies are present ensures personal pages are always generated fresh for that visitor.
Check whether any other plugins on your site set cookies that signal personalised content, like membership or multilingual plugins, and add them to the rule.
Step 3: Apply caching in the PHP location
In the PHP location block, turn on caching and apply the skip rules:
- fastcgi_cache_valid caches successful responses and redirects for 60 minutes. Don’t cache errors for long, or a temporary problem will be served to everyone.
- fastcgi_cache_bypass tells Nginx not to serve from cache when skip is set.
- fastcgi_no_cache tells Nginx not to save the response when skip is set.
- X-Cache adds a header showing whether each response was a HIT, MISS, BYPASS, EXPIRED or STALE. Invaluable for testing.
Remember the Nginx quirk that add_header in a location replaces headers set at server level. If you set security headers in the server block, repeat them here or use a shared include file.
Step 4: Test the configuration
Check the syntax and reload:
Then test as an anonymous visitor:
| <code>curl -sI https://example.com/ | grep -i x-cache |
|---|---|
| curl -sI https://example.com/ | grep -i x-cache</code> |
The first request should show MISS, the second HIT.
Then test the exclusions:
- Log in, and confirm pages show BYPASS and display the admin bar.
- Add a product to the cart in a private window, and confirm cart, checkout and other pages show BYPASS afterwards.
- Visit the cart and checkout directly, and confirm BYPASS.
- Submit a form, and confirm it works.
Test thoroughly before relying on it. Caching mistakes are among the most serious bugs a store can have.
Step 5: Purging the cache
When you publish or update content, cached pages need to be cleared, or visitors see old versions until the cache expires.
Options include:
- Short cache times for frequently updated sites, accepting a short delay before changes appear.
- A purge plugin that clears relevant cached pages when content changes. Some plugins work with an Nginx cache purge module, which allows targeted purging through special URLs, while others delete cache files directly from disk. Check which method your server supports.
- Manual purging by deleting the cache directory contents after major changes, like theme updates:
For most business sites, a purge plugin combined with a cache time of an hour or so works well. When a post is updated, the plugin clears that post, the homepage and relevant archives.
Common pitfalls
Caching personalised content
The most serious mistake. Always test logged-in and cart scenarios. If any plugin shows personalised content to anonymous visitors, like recently viewed products or location-based content, that content will be cached and shown to everyone. Such features need to load personalised parts with JavaScript after the page loads, or be excluded.
Caching nonces
WordPress nonces in cached pages can expire, causing forms or AJAX actions to fail for visitors who receive an old cached page. Most well-built plugins handle this, but if a form breaks only on cached pages, an expired nonce in cached HTML is a likely cause. Shorter cache times or loading such forms dynamically help.
Forgetting mobile variations
If your theme serves different HTML for mobile and desktop based on the user agent, rather than using responsive design, a single cache will serve one version to everyone. Most modern themes are responsive, so this is rarely a problem, but check.
Not monitoring disk space
The cache lives on disk. max_size prevents unlimited growth, but make sure the disk has room.
Stale content after updates
Without purging, visitors see old content until cache entries expire. Set up purging or accept the delay.
FastCGI cache versus plugin caching
FastCGI caching is faster than plugin-based page caching because Nginx serves cached pages without starting PHP at all. It’s well suited to servers you control. Plugin caching is easier on shared hosting, where you can’t change server configuration. Don’t run both page caches on the same site, as stacked caches cause confusing stale content.
Why I prefer server-level caching
On servers I manage, FastCGI caching has consistently handled traffic spikes better than plugin-based caching. During one client’s product launch, traffic jumped to many times its normal level within minutes. Because the product and landing pages were served from the Nginx cache, PHP and the database barely noticed. Only cart and checkout requests reached PHP, and the server had plenty of capacity for those.
The same server without caching would have needed far more PHP workers and memory, or it would have slowed to a crawl at exactly the moment the business needed it most. Server-level caching doesn’t replace good code and a well-tuned database, but it buys enormous headroom.
The trade-off is that it requires server access and careful configuration. On managed hosting, the host’s own caching layer usually plays this role, and you should use that instead.
Frequently asked questions
Is FastCGI caching safe for WooCommerce?
Yes, with correct exclusions for cart, checkout and account pages and for visitors with cart or login cookies. Test thoroughly.
How long should pages be cached?
For most sites, between 30 minutes and a few hours, combined with purging when content changes. Frequently updated sites may prefer shorter times.
Do I still need Redis with FastCGI caching?
Yes, for dynamic requests. FastCGI caching helps anonymous visitors; Redis speeds up everything that runs PHP, including logged-in users and checkouts.
How do I know if caching is working?
Check the X-Cache header. HIT means the page came from cache; MISS means it was generated and saved; BYPASS means caching was skipped.
Final thoughts
Nginx FastCGI caching is one of the most effective performance tools for WordPress on a server you control. Define a cache zone, build careful skip rules for logged-in users, carts and dynamic pages, add a cache status header, test every scenario and set up purging. Configured carefully, it serves pages in milliseconds and keeps your site steady under traffic that would otherwise overwhelm it.




