VPS

Nginx FastCGI Cache on VPS — Speed Up WordPress Efficiently

Updated Sep 20, 2026 · 12 min read · VPS / WordPress

Nginx FastCGI Cache setup on VPS to speed up WordPress

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.

How it works: Browser → Nginx → Check Cache → HIT: Serve cached file instantly / MISS: Forward to PHP-FPM → WordPress → MySQL → Store in cache → Return to Browser

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.

MetricWithout CacheWith FastCGI Cache
TTFB (Time to First Byte)200–800 ms5–30 ms
Requests per second30–1001,000–5,000+
CPU usage at traffic peak80–100%5–20%
Memory usageHigh (PHP workers)Very low
Database queries/request10–50 queries0 queries (cache HIT)
Best for: Static-heavy sites like blogs, news sites, portfolios, and landing pages. May not suit real-time applications like live chat or stock tickers.

System Prerequisites

Verify Nginx and PHP-FPM
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:

1Add fastcgi_cache_path to 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";
OptionValueMeaning
levels=1:21:2Subdirectory structure to distribute inodes evenly
keys_zoneWORDPRESS:100mZone name and memory size for storing keys (100 MB ≈ 800,000 keys)
inactive60mRemove cache entries not accessed within 60 minutes
max_size1gMaximum total disk size for all cached files
2Create the cache directory
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):

3Complete Nginx config for WordPress + FastCGI Cache
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.

Conditionskip_cacheReason
POST request1 (bypass)Form submission must be processed live
Query string present1 (bypass)May be pagination or filtered results
/wp-admin/ URL1 (bypass)Admin dashboard must always be real-time
Cookie: logged_in1 (bypass)Logged-in users must see live content
Cookie: woo_cart1 (bypass)Cart may change at any time
Regular GET request0 (cache)Anonymous visitors use cached pages

Testing and Verifying the Cache

4Test Nginx config and reload
sudo nginx -t
sudo systemctl reload nginx
5Check Cache HIT/MISS with curl
# 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:

  1. Install Nginx Helper from the WordPress Plugin Directory
  2. Go to Settings → Nginx Helper
  3. Enable Purge and select Delete local server cache files
  4. Set Cache Path to /var/cache/nginx/
  5. Select purge triggers: new post, post update, new comment
Manual cache clear: To purge all cached files immediately: sudo find /var/cache/nginx -type f -delete

FastCGI Cache vs Other Caching Methods

Cache MethodSpeedComplexityCostBest For
FastCGI Cache⚡⚡⚡⚡⚡ModerateFreeAny VPS
W3 Total Cache⚡⚡⚡EasyFree/ProShared hosting
WP Rocket⚡⚡⚡⚡Very easy$59/yrBeginners
Redis Object Cache⚡⚡⚡⚡ModerateFreeComplement to FastCGI
Varnish Cache⚡⚡⚡⚡⚡HardFreeVery 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_path to nginx.conf inside http {}
  • Configure virtual host with complete bypass rules
  • Create cache directory with correct ownership
  • Test with curl — verify X-Cache: HIT on 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.