How to Configure Nginx Fastcgi Cache for WordPress Full-page Caching on Ubuntu
Learning how to configure Nginx FastCGI cache for WordPress full-page caching on Ubuntu is one of the best ways to dramatically speed up your WordPress site. FastCGI caching stores fully rendered HTML pages on disk. When a visitor requests a page, Nginx serves the cached version directly. PHP and MySQL never get touched. The result is near-instant page delivery and a massive drop in server load. This tutorial walks you through every step , from installing the required packages to writing the Nginx configuration and verifying that caching is working correctly. By the end, you’ll have a production-ready full-page cache running on your Ubuntu server.
Prerequisites for Configuring Nginx FastCGI Cache on Ubuntu
Before you start, make sure you have the following in place:
Required software and access:
- Ubuntu 20.04 or 22.04 (fresh or existing install)
- Nginx installed and running
- PHP-FPM installed (PHP 8.1 or 8.2 recommended)
- WordPress installed and functional
- Root or sudo access to the server
Assumed knowledge: You should be comfortable with the Linux command line, editing files with a text editor like nano, and basic Nginx configuration. You don’t need to be an expert, but you should know how to SSH into your server.
Estimated time: 30–45 minutes.
You’ll also want a domain pointed at your server so you can test caching headers in a browser. If you haven’t set up Nginx and PHP-FPM yet, check the official Nginx documentation before continuing.
How to Configure Nginx FastCGI Cache for WordPress Step by Step
You might also find this useful: VPN on Linode using Debian (PPTP)
Follow these steps carefully. Each one builds on the last, so don’t skip ahead.
Step 1: Create the FastCGI cache directory
Nginx needs a place on disk to store cached pages. Create that directory now and give Nginx ownership of it.
sudo mkdir -p /var/cache/nginx/fastcgi
sudo chown www-data:www-data /var/cache/nginx/fastcgi
Step 2: Define the FastCGI cache zone in nginx.conf
Open your main Nginx configuration file:
sudo nano /etc/nginx/nginx.conf
Add the following line inside the http {} block, before any server {} blocks:
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
Save and close the file. This line tells Nginx where to store cached files, how to organize them, and how much memory to use for the cache index.
Step 3: Configure your WordPress server block
Open your WordPress site’s Nginx server block. It’s usually located at /etc/nginx/sites-available/yourdomain.com:
sudo nano /etc/nginx/sites-available/yourdomain.com
Add the following variables near the top of the server {} block, before the location directives:
set $skip_cache 0;
# Don't cache POST requests
if ($request_method = POST) { set $skip_cache 1; }
# Don't cache URLs with query strings
if ($query_string != "") { set $skip_cache 1; }
# Don't cache logged-in users or recent commenters
if ($http_cookie ~ "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
Step 4: Update the PHP location block
Find the location ~ .php$ block in your server config. Add these FastCGI cache directives inside it:
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout updating http_500 http_503;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-FastCGI-Cache $upstream_cache_status;
The X-FastCGI-Cache header is useful for debugging. It will show HIT, MISS, or BYPASS in your browser’s developer tools.
Step 5: Test and reload Nginx
Always test your configuration before reloading. A syntax error will take your site offline.
sudo nginx -t
If you see syntax is ok, reload Nginx:
sudo systemctl reload nginx
Step 6: Install the Nginx Helper plugin in WordPress
The Nginx Helper plugin lets WordPress automatically purge cached pages when you publish or update content. Install it from your WordPress dashboard under Plugins → Add New. Search for “Nginx Helper” and install it.
After activation, go to Settings → Nginx Helper. Enable cache purging and select Delete local server cache files. Set the cache path to /var/cache/nginx/fastcgi.
Step 7: Verify caching is working
Use curl to check the cache status header:
curl -I https://yourdomain.com
Look for this line in the output:
X-FastCGI-Cache: MISS
Run the same command a second time. You should now see:
X-FastCGI-Cache: HIT
A HIT confirms that Nginx is serving the cached page. Your setup is working correctly.
Troubleshooting Common Nginx FastCGI Cache Problems
Even with careful setup, things can go wrong. Here are the most common issues and how to fix them.
Cache always shows MISS or BYPASS
This usually means your $skip_cache conditions are triggering. Check if you’re logged into WordPress in the same browser you’re testing with. Log out and test again. Also check that no extra cookies are being set by plugins or themes.
Nginx fails the config test
Run sudo nginx -t and read the error output carefully. A missing semicolon or a misplaced directive is the most common cause. Double-check that you placed the fastcgi_cache_path directive inside the http {} block, not inside a server {} block.
Cache directory permission errors
If Nginx can’t write to the cache directory, you’ll see errors in /var/log/nginx/error.log. Fix permissions with:
sudo chown -R www-data:www-data /var/cache/nginx/fastcgi
Pages not updating after content changes
Make sure the Nginx Helper plugin is active and configured correctly. If you’re not using the plugin, you can manually clear the cache by deleting the cache directory contents:
sudo rm -rf /var/cache/nginx/fastcgi/
Then reload Nginx to let it rebuild the cache directory structure on the next request.
WooCommerce or cart pages getting cached
Add these conditions to your $skip_cache block to exclude WooCommerce pages:
if ($request_uri ~ "/(cart|checkout|my-account)") { set $skip_cache 1; }
This prevents checkout and account pages from being served from cache, which would break session-based functionality.
Conclusion: What You’ve Accomplished
You now know how to configure Nginx FastCGI cache for WordPress full-page caching on Ubuntu from start to finish. Your server now stores rendered HTML pages on disk and serves them without touching PHP or your database. That means faster load times, lower CPU usage, and a better experience for every visitor. From here, you can explore combining this setup with a CDN for even faster global delivery. You might also look at tuning your inactive and max_size cache parameters based on your server’s available disk space and traffic patterns. This FastCGI caching setup scales well and works reliably in production environments of all sizes.
