Practical DevOps
Configuring NGINX and Redis for High-Traffic WordPress and CMS Workloads
A conservative starting point for full-page caching, persistent object caching and database tuning, with explicit checks for private content.
1. Measure uncached requests first
Record request rate, p95 response time, PHP-FPM queueing, database time and memory usage on a staging copy. A full-page cache avoids PHP for eligible responses; Redis object caching can reduce repeated database work when PHP still runs. PHP OPcache serves another purpose by retaining compiled PHP code.
Use a supported NGINX, PHP and CMS release. Size PHP-FPM workers from measured process memory and available RAM after reserving space for the database, Redis and the OS. On multi-socket hosts, inspect NUMA locality and network IRQ distribution before changing affinity.
2. Start with a conservative cache policy
The following fragments are a staging example for a conventional WordPress front controller. They cache only anonymous GET/HEAD requests without cookies, authorisation or query strings. Extend the excluded paths for your site’s checkout, account, membership, language and other personalised routes before enabling caching.
Place this block inside NGINX’s http context. Create the cache directory with write access for the NGINX worker user. The sample cache size and TTL are starting values for testing.
map $request_method $skip_method {
default 1;
GET 0;
HEAD 0;
}
map $http_cookie $skip_cookie { default 1; "" 0; }
map $http_authorization $skip_auth { default 1; "" 0; }
map $args $skip_query { default 1; "" 0; }
map $request_uri $skip_path {
default 0;
~*^/(wp-admin|wp-login\.php|wp-json|xmlrpc\.php|cart|checkout|my-account)(/|\?|$) 1;
}
fastcgi_cache_path /var/cache/nginx/cms levels=1:2
keys_zone=CMS:32m inactive=30m max_size=1g;
3. Apply caching to the front controller
Merge this into your existing site’s server block, retaining its TLS, document root, access restrictions and separate handling of other PHP scripts. Replace the example PHP socket with the installed pool’s socket. Do not add a duplicate location block.
Bypass prevents reading cached content; no-cache prevents storing the response. Keep both. Preserve NGINX’s normal handling of Set-Cookie and Cache-Control; do not force private responses into the cache.
location / {
try_files $uri $uri/ /index.php?$args;
}
location = /index.php {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_pass unix:/run/php/REPLACE-WITH-YOUR-FPM-SOCKET.sock;
fastcgi_cache CMS;
fastcgi_cache_key "$scheme|$request_method|$host|$request_uri";
fastcgi_cache_valid 200 30s;
fastcgi_cache_lock on;
fastcgi_cache_bypass $skip_method $skip_cookie $skip_auth $skip_query $skip_path;
fastcgi_no_cache $skip_method $skip_cookie $skip_auth $skip_query $skip_path;
add_header X-Cache-Status $upstream_cache_status always;
}
Run nginx -t before reloading the staging service. Start with the short TTL; add a tested publishing invalidation workflow before using longer lifetimes. A CMS edit does not automatically purge this NGINX cache.
The example’s add_header can override inherited add_header directives under NGINX’s default inheritance rules. Preserve the site’s required security headers in this location and verify the actual response headers before deployment.
4. Enable persistent object caching
For a staging WordPress installation using the Redis Object Cache plugin, place the settings below in wp-config.php before WordPress loads. Use a private Redis instance dedicated to this site, then activate the plugin and enable its object-cache drop-in. A prefix avoids accidental key collisions but is not a security boundary.
Bind Redis to loopback or an authorised private interface, restrict access and configure ACL authentication as appropriate. Use TLS for remote connections. Keep credentials outside published examples and separate untrusted sites into separate instances or enforceable access boundaries.
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PREFIX', 'cms-staging:');
Set a measured memory limit and a cache-appropriate eviction policy on a cache-only Redis instance. Do not share that eviction configuration with durable queues or application records. Verify the plugin’s connected status, key activity and behaviour when Redis is unavailable.
5. Fix expensive database work
Use a bounded slow-query logging window and protect the resulting logs, which may contain user data. Review repeated queries with EXPLAIN on a representative database. Check indexes, rows examined, plugin queries and oversized autoloaded options before increasing server capacity.
Apply schema changes on staging with a rollback plan. A fast cached homepage can hide a slow administration area or checkout. Measure those uncached paths separately and size the database buffer pool alongside the rest of the host’s memory demand.
6. Check correctness before load testing
For a public test page, repeat the request and inspect X-Cache-Status. Use two separate browser sessions to confirm private data never crosses users. Test these cases before production rollout:
- An eligible public page changes from MISS to HIT; cookie, authorisation, query-string and excluded-path requests remain BYPASS.
- Login, logout, carts, previews, forms and API responses remain correct, including the site’s custom routes.
- Publishing and editing show updated content within the agreed freshness window.
- Cold-cache traffic and Redis interruption do not exhaust PHP workers or expose cached personal data.