Nginx FastCGI Cache Setup: Full-Page Caching Without a Plugin

A WordPress site on a small VPS can feel slow even when the server has plenty of CPU headroom. The usual cause is PHP: every visit runs WordPress, hits the database, and builds the page from scratch. A nginx fastcgi cache setup fixes that at the web server level. Nginx stores the finished HTML page and serves it from disk on the next visit, so PHP and MySQL never wake up. No plugin, no extra service, and it survives WordPress updates because it lives outside WordPress.

This guide walks through the full setup on Ubuntu 24.04 with PHP-FPM: the cache zone, the rules that decide what gets cached, how to keep logged-in users and carts out of it, and how to check it is actually working. If you are still tuning the PHP side, read our guide to PHP-FPM pool settings first, since a slow pool still hurts the first uncached hit.

What FastCGI cache does, and what it skips

When PHP-FPM builds a page, Nginx can save a copy of the response. The next visitor asking for the same URL gets that saved copy in a few milliseconds. The win is biggest on pages that rarely change. A cached page skips PHP, the database, and every WordPress plugin hook.

It is not a fix for everything. Pages that differ per visitor, like the admin area, the cart, or a logged-in dashboard, must never be served from a shared cache, or one visitor will see another visitor’s page. That is why the bypass rules in step 3 matter more than the cache itself. FastCGI cache only caches GET and HEAD requests by default. For the bigger picture, our explainer on proxy cache vs FastCGI cache covers when each one fits.

Nginx FastCGI Cache Setup in Four Steps

Four moves: define a cache zone, store the right responses, skip the requests that must stay dynamic, and clear stale pages when content changes.

Before you start: check your stack

Run nginx -V and confirm your build has the cache module; stock Ubuntu Nginx does. Check that your site already works over PHP-FPM and that nginx -t passes. Note your PHP-FPM socket path, usually /run/php/php8.3-fpm.sock, and your site’s server block file under /etc/nginx/sites-available/. Back it up before you edit it. If your site sits behind a CDN, the CDN may cache pages too; our comparison of Cloudflare APO vs server-side caching explains how the two layers interact.

Step 1: define the cache zone

Open /etc/nginx/nginx.conf and add a fastcgi_cache_path line inside the http block, outside any server block:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WP:100m inactive=60m max_size=1g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

The zone name WP is yours to pick; 100m is the key index size in shared memory, not disk space. inactive=60m drops pages nobody asked for in the last hour, and max_size=1g caps disk use. A busy site with many URLs will need more than 1 GB, and the oldest entries get evicted first. Create the directory and give Nginx ownership:

sudo mkdir -p /var/cache/nginx
sudo chown www-data:www-data /var/cache/nginx
sudo nginx -t && sudo systemctl reload nginx

Step 2: cache the right responses only

Inside your site’s PHP location block, usually location ~ .php$, add the cache directives after the fastcgi_pass line:

fastcgi_cache WP;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-FastCGI-Cache $upstream_cache_status;

fastcgi_cache_valid sets how long each status code stays cached: one hour for normal pages, one minute for 404s so a typo does not stick around. The result is an nginx full page cache for anonymous visitors. The X-FastCGI-Cache header is your debug tool: MISS on the first visit, HIT on later ones, BYPASS when a rule skips the cache. The $skip_cache variable does not exist yet; step 3 defines it, so do not reload until then or Nginx will fail the config test.

Step 3: skip the cache for logged-in users, carts, and POST requests

This block stops the classic FastCGI cache bug: a visitor seeing someone else’s cart, or an admin seeing a stale page. Put it inside your server block, above the PHP location:

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|sitemap(_index)?.xml") {
    set $skip_cache 1;
}

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

Each line covers one case. POST requests always run PHP, because they change state. Any URL with a query string skips the cache, which protects search results and filtered views. The admin area, xmlrpc, feeds, and sitemaps stay dynamic. The cookie line is the key one for WordPress: anyone with a login cookie, a comment author cookie, or a post password cookie gets a fresh page. A nginx cache bypass like this costs nothing on cached traffic, since Nginx checks the rules before it ever touches PHP.

WooCommerce and other shops need more rules: skip /cart/, /checkout/, and /my-account/, plus the woocommerce_items_in_cart cookie. If a form shows a success message after submit, the POST rule already covers it, but test the flow once by hand.

Step 4: purge stale pages when content changes

Stock Nginx has no built-in purge URL for FastCGI cache, which surprises people coming from caching plugins. You have three options.

First, the blunt one: flush the whole cache when you publish or update a post. Fine for a small site that updates a few times a day:

sudo rm -rf /var/cache/nginx/*
sudo systemctl reload nginx

Second, a fastcgi cache purge by key. The cache key is the MD5 of $scheme$request_method$host$request_uri, and the file lives under the two-level directory tree. Find it with grep -r on the URL string inside the cache files, then delete just that file. It works, but script it rather than doing it by hand each time.

Third, the nginx cache purge module (ngx_cache_purge), which adds a fastcgi_cache_purge directive. It is not in the stock Ubuntu package, so you either build Nginx from source or use a package that ships it, such as the one from the Nginx mainline repo. Lock the purge location to your own IP, or anyone can wipe your cache.

For most WordPress sites, the practical answer is a small helper plugin that runs the flush command over SSH or hits a secured endpoint, with the 60-minute TTL as a safety net. Even if a purge fails, the stale page disappears within the hour.

How to check the cache is working

Load a post twice and read the response headers:

curl -sI https://example.com/a-post/ | grep -i x-fastcgi-cache

The first request should show MISS, the second HIT. Load /wp-admin/ while logged in and confirm it shows BYPASS. A HIT should come back in well under 100 ms on a local VPS, while a MISS takes as long as PHP needs. If every request shows MISS, look for a cookie in the request, a Set-Cookie header in the response, or a Cache-Control: private header from a plugin. Nginx refuses to cache responses that set cookies or mark themselves private, so find the plugin sending those headers before you blame the config.

Common mistakes that break FastCGI cache

  • Caching the admin area. Missing the cookie rule means editors see stale pages and visitors can be served a cached admin page. Test logged-in and logged-out in two browsers.
  • Forgetting the query string rule. Without it, a search for “blue shoes” can return the cached result for “red shoes” to the next visitor.
  • Setting the TTL too long. A 24-hour cache means edits sit invisible all day on a news site. Match the TTL to how often the site changes.
  • Caching pages that set cookies. A plugin that sets a cookie on every page, even for anonymous visitors, silently disables the cache. Check the response headers first.
  • No cache header in production. The X-FastCGI-Cache header is small and useful; keep it while you tune, and remove it later if you prefer not to expose it.

FastCGI cache vs a caching plugin

A WordPress caching plugin is easier to purge and usually handles the bypass rules for you, but it still boots WordPress to decide whether to serve a cached page. FastCGI cache answers before PHP starts, so it is faster and uses less CPU under traffic spikes. The trade is control: plugins give you a dashboard button, FastCGI cache gives you config files and a command line. If you run several WordPress sites on one VPS, one shared cache zone serves them all, and a fastcgi cache wordpress setup like this one scales better than a plugin per site. If you are weighing the plugin route instead, see how FastCGI cache compares with LiteSpeed Cache for a side-by-side of the two approaches.

Start with a short TTL, test the bypass rules logged in and logged out, and watch the X-FastCGI-Cache header for a day. Once HITs outnumber MISSes, the cache is doing its job.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.