
Table of Contents
- What is Nginx FastCGI Cache?
- Why Use FastCGI Cache with WordPress?
- System Prerequisites
- Configuring fastcgi_cache_path
- Virtual Host Configuration for WordPress
- Bypass Cache Rules
- Testing and Verifying the Cache
- Auto-Purge Cache with Nginx Helper
- FastCGI Cache vs Other Caching Methods
- Common Troubleshooting
- Summary
- Frequently Asked Questions
What is Nginx FastCGI Cache?
Nginx FastCGI Cache is a server-level caching system built into Nginx. It stores responses from PHP-FPM as files on disk (or in memory). When the same URL is requested again, Nginx serves the cached file immediately — without invoking PHP-FPM or querying MySQL at all.
FastCGI Cache has been built into Nginx since version 0.7.48, requiring no additional modules. With the right configuration, it works seamlessly with WordPress.
Why Use FastCGI Cache with WordPress?
WordPress is inherently dynamic — every request triggers PHP execution and database queries. On high-traffic sites, this consumes significant server resources. FastCGI Cache eliminates this overhead for cached pages.
| Metric | Without Cache | With FastCGI Cache |
|---|---|---|
| TTFB (Time to First Byte) | 200–800 ms | 5–30 ms |
| Requests per second | 30–100 | 1,000–5,000+ |
| CPU usage at traffic peak | 80–100% | 5–20% |
| Memory usage | High (PHP workers) | Very low |
| Database queries/request | 10–50 queries | 0 queries (cache HIT) |
System Prerequisites
- VPS Linux (Ubuntu 20.04/22.04/24.04 or Debian 11/12)
- Nginx 1.18+ (check with
nginx -v) - PHP-FPM (not mod_php)
- WordPress installed and running
- Root or sudo access on the VPS
nginx -v
php-fpm8.1 -v
systemctl status nginx php8.1-fpm
Configuring fastcgi_cache_path
The first step is defining the cache zone in the main Nginx configuration. Add this inside the http {} block in /etc/nginx/nginx.conf:
sudo nano /etc/nginx/nginx.conf
Add inside the http { } block:
fastcgi_cache_path /var/cache/nginx
levels=1:2
keys_zone=WORDPRESS:100m
inactive=60m
max_size=1g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
| Option | Value | Meaning |
|---|---|---|
| levels=1:2 | 1:2 | Subdirectory structure to distribute inodes evenly |
| keys_zone | WORDPRESS:100m | Zone name and memory size for storing keys (100 MB ≈ 800,000 keys) |
| inactive | 60m | Remove cache entries not accessed within 60 minutes |
| max_size | 1g | Maximum total disk size for all cached files |
sudo mkdir -p /var/cache/nginx
sudo chown www-data:www-data /var/cache/nginx
Virtual Host Configuration for WordPress
Edit the virtual host file for your WordPress site (typically at /etc/nginx/sites-available/yoursite.com):
server {
listen 443 ssl http2;
server_name yoursite.com www.yoursite.com;
root /var/www/yoursite.com/public;
index index.php;
# FastCGI Cache bypass logic
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/|index.php") {
set $skip_cache 1;
}
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in|woocommerce_items_in_cart|woocommerce_cart_hash") {
set $skip_cache 1;
}
location / { try_files $uri $uri/ /index.php?$args; }
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache $upstream_cache_status;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
}
Bypass Cache Rules
Correct bypass rules are critical for a safe FastCGI Cache setup.
| Condition | skip_cache | Reason |
|---|---|---|
| POST request | 1 (bypass) | Form submission must be processed live |
| Query string present | 1 (bypass) | May be pagination or filtered results |
| /wp-admin/ URL | 1 (bypass) | Admin dashboard must always be real-time |
| Cookie: logged_in | 1 (bypass) | Logged-in users must see live content |
| Cookie: woo_cart | 1 (bypass) | Cart may change at any time |
| Regular GET request | 0 (cache) | Anonymous visitors use cached pages |
Testing and Verifying the Cache
sudo nginx -t
sudo systemctl reload nginx
# First request — should return MISS
curl -I https://yoursite.com/ | grep X-Cache
# Second request — should return HIT
curl -I https://yoursite.com/ | grep X-Cache
Expected output:
X-Cache: MISS ← first request
X-Cache: HIT ← subsequent requests
Auto-Purge Cache with Nginx Helper
Install the Nginx Helper WordPress plugin to automatically purge cache when content is updated:
- Install Nginx Helper from the WordPress Plugin Directory
- Go to Settings → Nginx Helper
- Enable Purge and select Delete local server cache files
- Set Cache Path to
/var/cache/nginx/ - Select purge triggers: new post, post update, new comment
sudo find /var/cache/nginx -type f -delete
FastCGI Cache vs Other Caching Methods
| Cache Method | Speed | Complexity | Cost | Best For |
|---|---|---|---|---|
| FastCGI Cache | ⚡⚡⚡⚡⚡ | Moderate | Free | Any VPS |
| W3 Total Cache | ⚡⚡⚡ | Easy | Free/Pro | Shared hosting |
| WP Rocket | ⚡⚡⚡⚡ | Very easy | $59/yr | Beginners |
| Redis Object Cache | ⚡⚡⚡⚡ | Moderate | Free | Complement to FastCGI |
| Varnish Cache | ⚡⚡⚡⚡⚡ | Hard | Free | Very high traffic |
Common Troubleshooting
X-Cache always returns MISS
A browser cookie may be triggering the bypass condition. Check with:
curl -s -D - https://yoursite.com/ -o /dev/null | grep -E "X-Cache|Set-Cookie"
If Set-Cookie appears on every request, add fastcgi_ignore_headers Set-Cookie; to the PHP location block.
Stale content showing after post update
sudo find /var/cache/nginx -type f -delete
sudo systemctl reload nginx
WooCommerce cart showing wrong items
Ensure bypass rules include woocommerce_items_in_cart and woocommerce_cart_hash cookies.
Summary
Nginx FastCGI Cache Setup Checklist
- Add
fastcgi_cache_pathtonginx.confinsidehttp {} - Configure virtual host with complete bypass rules
- Create cache directory with correct ownership
- Test with curl — verify
X-Cache: HITon second request - Install Nginx Helper plugin for automatic cache purging
- Monitor cache size with
du -sh /var/cache/nginx/
Nginx FastCGI Cache is one of the most cost-effective ways to dramatically speed up WordPress on a VPS. It requires no extra cost, one-time configuration, and can reduce TTFB from 500ms to under 30ms in production environments.
Need a VPS for Nginx + WordPress?
AsiaGB VPS Thailand — high-speed SSD, 99% Uptime SLA, Nginx-ready. Start from affordable monthly rates.
View VPS Plans →Frequently Asked Questions
What is the difference between Nginx FastCGI Cache and WordPress cache plugins?
FastCGI Cache operates at the server level before PHP is invoked. Nginx serves cached files directly without loading WordPress, PHP, or querying the database. WordPress plugins still require PHP to bootstrap before serving cached content.
Will FastCGI Cache serve stale content to visitors?
Not with proper configuration. Cache entries expire after the inactive period, and the Nginx Helper plugin purges cache automatically whenever content is updated.
What VPS size do I need?
Any VPS works. For high-traffic sites, 2 GB RAM is recommended. The cache itself is stored on disk, so RAM requirements are minimal.
Will logged-in users receive cached pages?
No — the bypass rules check for wordpress_logged_in_* cookies and skip the cache, ensuring authenticated users always receive live content.
How do I know if a page is being served from cache?
Check the X-Cache response header. HIT = served from cache; MISS = PHP processed the request. Use curl -I https://yoursite.com.
Does FastCGI Cache work with WooCommerce?
Yes, with proper bypass rules for cart, checkout, account pages, and logged-in users. Cart contents and pricing will always be accurate.