How to Configure Nginx FastCGI Cache for WordPress Without a Plugin

A nginx fastcgi cache wordpress setup stores finished HTML pages in memory or on disk, so Nginx can serve repeat visits without asking PHP to rebuild the page each time. The result is faster time to first byte on a VPS that already runs Nginx, and it costs no plugin subscription. This guide walks through the full setup: the cache zone, the bypass rules, the headers, and the purge step you run after every change.

This guide assumes you already run WordPress on a LEMP stack. If you still need the base install, follow a LEMP stack WordPress install on a VPS first, then come back here. You also need root or sudo access and a working Nginx test command before you touch any live config.

Nginx FastCGI Cache WordPress: How It Works

When a visitor requests a page, Nginx checks the cache first. On a cache miss, the request goes to PHP-FPM, WordPress builds the page, and Nginx stores a copy. On the next request for the same URL, Nginx serves the stored copy and PHP never runs. That is the whole mechanism, and it is why the bypass rules matter more than the cache itself: any page that differs per visitor must never enter the cache.

FastCGI cache sits at the server level, below WordPress. It does not know about your theme, your plugins, or your users. It only knows URLs, cookies, and request methods. Your config must translate WordPress behavior into those three signals. Get that translation right and the cache is safe. Get it wrong and logged-in users see each other’s admin bars, or shoppers see another person’s cart.

Before You Start: Backups and a Test Plan

Back up the Nginx site config before you edit it. One command saves you from a long night:

cp /etc/nginx/sites-available/wordpress /etc/nginx/sites-available/wordpress.bak
nginx -t

Run nginx -t before every reload, without exception. A config error that passes your editor can still stop Nginx from reloading, and on a live site that means downtime until you fix it. Test on a staging copy of the site first if you have one, and keep a second SSH session open while you reload, so a bad reload does not lock you out of your only terminal.

Also decide your purge method before you enable the cache. A cache you cannot clear is a liability: after you edit a post, change a menu, or update a plugin, stale pages keep serving until the cache lifetime expires. The purge section below gives you a simple method that works without extra modules.

Step 1: Define the Cache Zone

Open the Nginx http context, usually /etc/nginx/nginx.conf, and add a cache path. The keys_zone holds cache keys in shared memory, and inactive sets how long an unused entry stays before Nginx drops it:

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

Create the directory and give Nginx ownership, then test:

mkdir -p /var/cache/nginx
chown -R www-data:www-data /var/cache/nginx
nginx -t

The 100 MB zone holds keys, not page bodies, so it covers a large site. Page bodies live in the cache directory on disk. If your VPS disk is small, watch /var/cache/nginx with df -h during the first week, and lower inactive if it grows faster than you expect.

Step 2: FastCGI Cache Bypass Rules for Logged-In Users and Carts

FastCGI cache bypass rules are the safety net of the whole setup. Inside your server block, set a $skip_cache variable that defaults to 0, then flip it to 1 for every request that must reach PHP:

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; }

The cookie rule is the one that protects logged-in users. WordPress sets a wordpress_logged_in cookie when someone signs in, so any request carrying it skips the cache. The wp-postpass cookie does the same for password-protected posts. If you run WooCommerce, add its cart and session cookies to the same rule, and exclude /cart/, /checkout/, and /my-account/ from caching in the URI rule, because those pages are personal by definition.

A warning on if inside Nginx location blocks: keep these if statements at server level, where they only set a variable. Do not put rewrites or proxy passes inside them. The Nginx docs warn that if inside a location block behaves in surprising ways, and the variable-only pattern here stays on the safe side of that warning.

Step 3: Enable the Cache in the PHP Location Block

Inside the location block that passes requests to PHP-FPM, add the cache directives:

fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 60m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-FastCGI-Cache $upstream_cache_status;

fastcgi_cache_bypass tells Nginx to skip reading the cache, and fastcgi_no_cache tells it not to store the response. You need both: bypass alone still lets a logged-in page get stored for the next anonymous visitor. The X-FastCGI-Cache header shows HIT, MISS, or BYPASS on every response, which is your main test tool. Remove that header later if you do not want visitors to see cache status, but keep it while you test.

Reload and test with curl:

nginx -t && systemctl reload nginx
curl -I https://example.com/a-post/
curl -I https://example.com/a-post/

The first response should show MISS, the second HIT. That HIT confirms the wordpress page cache nginx layer works before you move on. If the second still shows MISS, check that the cache path exists, that Nginx owns it, and that no cookie rule is catching your test request. A browser sends cookies, so curl without cookies is the cleaner test. This server-level approach pairs well with a standard stack choice, and if you still weigh options, a LEMP versus LAMP comparison for WordPress hosting explains why Nginx fits this pattern better than Apache for most VPS setups.

An Nginx Cache Purge WordPress Workflow After Every Change

An nginx cache purge wordpress routine needs to run after you publish, update, or change site settings. Without the third-party purge module, the simplest reliable method is to delete the cache directory contents and reload:

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

This clears everything, so the next visit to each page rebuilds it. On a busy site that causes a short burst of PHP load, so purge during a quiet period when you can, and never purge on a timer every few minutes. A full purge after a content change is normal. A full purge every hour means your cache lifetime is wrong, and you should fix the lifetime instead.

If you want per-URL purges later, compile or install the ngx_cache_purge module, which adds a purge location you can call with curl. That is an extra build step, so start with the full purge, confirm the site behaves, and add per-URL purging only when full purges start to hurt.

Common Problems and Fixes

Stale pages after edits mean the purge step did not run, or the cache lifetime is too long for your publishing pace. Cut fastcgi_cache_valid to 10 minutes while you test, confirm fresh edits appear after a purge, then raise it back to 60 minutes.

Logged-in users seeing cached pages mean the cookie rule missed a cookie. Inspect the request cookies in your browser dev tools, add the missing cookie name to the rule, test with nginx -t, and reload. Never test this fix by logging out alone: log in as a second user and confirm the admin bar matches that user.

Cache MISS on every request usually means the cache path has wrong ownership, the zone name in the location block does not match the zone name in fastcgi_cache_path, or a set $skip_cache 1 rule catches every request. Check the zone name first, since a typo there fails silently.

One more scope note: this guide covers a single Nginx server. If you run a reverse proxy in front of WordPress, cache at one layer only, or the two caches will fight over stale content. A comparison of reverse proxy options helps you decide where that layer should sit, and a Traefik versus Nginx Proxy Manager guide for Docker beginners covers the container side if your stack runs in Docker. For the official directive reference, see the Nginx FastCGI module docs.

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.