WordPress generates each page using PHP and multiple MySQL queries, even if the page is identical for all visitors. The Nginx FastCGI cache stores the pre-generated page and serves it directly on subsequent visits, without going through PHP. For public pages, the response time typically drops from hundreds of milliseconds to just a few.
Is this cache useful to you?
- This is useful for: blogs, corporate websites, and online stores with a lot of anonymous traffic, hosted on a VPSwith Nginx and PHP-FPM.
- This works—with some caution: WooCommerce stores and forums. It works, but you have to exclude the cart, checkout, and customer account; the settings in this guide already do that.
- This won't work for you: if your website runs on Apache or if you already have another full-page cache plugin enabled. Having two page caches at the same time complicates every change.
1. Define the cache area
Create /etc/nginx/conf.d/fastcgi-cache.conf. The files in that folder are loaded into block http:
fastcgi_cache_path /var/cache/nginx/wordpress levels=1:2 keys_zone=WORDPRESS:100m max_size=1g inactive=60m use_temp_path=off;fastcgi_cache_key "$scheme$request_method$host$request_uri";keys_zone=WORDPRESS:100m: memory for the cache index. Each megabyte stores about 8,000 keys.max_size=1g: maximum disk space.inactive=60m: Deletes pages that no one has requested within an hour.
2. Decide what is not cached
In block server on your website, before blocks location, add the exclusion rules. Logged-in users, users who have left comments, the admin panel, and the checkout process should always receive content generated in real time:
set $skip_cache 0;if ($request_method = POST) { set $skip_cache 1; }if ($query_string != "") { set $skip_cache 1; }if ($request_uri ~* "/wp-admin|/xmlrpc.php|wp-.*\.php|/feed|sitemap(_index)?\.xml|/cart|/checkout|/my-account") { set $skip_cache 1; }if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash|wp_woocommerce_session_") { set $skip_cache 1; }In the third rule, enter your store's actual URLs. If they are in Spanish, such as /carrito, /finalizar-compra, or /mi-cuenta, add them without the trailing slash so that they match both with and without it.
3. Enable caching in the PHP block
In the location ~ \.php$ block you already have, below fastcgi_pass, add:
fastcgi_cache WORDPRESS;fastcgi_cache_valid 200 301 302 60m;fastcgi_cache_bypass $skip_cache;fastcgi_no_cache $skip_cache;fastcgi_cache_use_stale error timeout updating http_500 http_503;fastcgi_cache_lock on;add_header X-Cache $upstream_cache_status;ote: If you have security headers in block server that include add_header, they will no longer be sent in PHP responses because Nginx does not inherit add_header in a block location that defines its own headers. Repeat them in this block or move them to a file that you include in both.
Check the settings and apply them using reload, which does not terminate open connections:
sudo nginx -t && sudo systemctl reload nginx4. Check to see if it works
Request the homepage twice in a row using a normal GET request and look at the X-Cache header. Do not use curl -I: it sends a HEAD request, and the cache key includes the method:
curl -s -o /dev/null -D - https://example.com/ | grep -i x-cachecurl -s -o /dev/null -D - https://example.com/ | grep -i x-cacheMISSand thenHIT: the cache works.BYPASS: An exclusion rule is being applied. This is expected in three cases: within/wp-admin/, on any page if you're logged in, and on any page if you have items in your cart. Please also verify this by testing it in a live session on your store.- Always
MISS: Check to see if WordPress or a plugin is sendingSet-CookieorCache-Control: no-cache(orprivate) on every page; these are the most common causes, because Nginx does not cache those responses.
5. Clear the cache when you publish
With this configuration, it may take up to 60 minutes for a new or edited post to appear. To manually clear the entire cache after a major change:
sudo find /var/cache/nginx/wordpress -type f -deleteTo do this automatically, the Nginx Helper plugin can delete the affected pages every time you publish. Configure it to use the same cache folder and follow its documentation for the purge method.
ext step
Caching takes some of the load off PHP and MySQL, but it won't fix a slow plugin or a poorly indexed database: if the website is still slow while logged in, the bottleneck is somewhere else. And if you're running low on resources, choose a Cloud VPS with more CPU and memory.