Using Nginx with W3 Total Cache and Memcached

When working on a WordPress project recently where speed was a huge concern, I relied on everyone’s favorite reverse proxy, Nginx, to offload the work required by Apache to serve pages. This worked great, but an issue I encountered was invalidating cached items that were just updated (e.g. comments, post edits, etc.) in Nginx. For users to see the updated content, the cache TTL had to pass, which was unacceptable for our purposes.

I was already using W3 Total Cache in WordPress with Memcached storage, and although W3 Total Cache knew to invalidate certain cached items in Memcached, Nginx was not aware of this since it used its own local cache.

Nginx, thankfully, has built-in support for Memcached, and is therefore able to get key/value pairs that were previously set by the application (i.e. by default (without patching), it doesn’t store cached items to Memcached, but can read from Memcached). My hope was to let W3 Total Cache perform all the caching/invalidation, and let Nginx rely on that cache for content.

The problem, though, is that W3 Total Cache stores items in Memcached using a nonstandard keys, and stores the value as a PHP array. It also compresses values (via the Memcache PHP library) that are over a particular size. These incompatibilities made working with Nginx not possible–at least not without modification.

I took the time to modify W3 Total Cache so that it stores data in a format that Nginx can clearly understand. Once done, it was awesome. Comments appeared immediately, and pages were snappy.

I’ve upped the modified W3 Total Cache plugin here:

You will need to set a key to use (e.g. ‘’). Memcached keys will then look like ‘’. In Nginx, you would set the key accordingly (set $memcached_key$request_uri;).


2 comments on “Using Nginx with W3 Total Cache and Memcached
  1. JonG says:

    Perhaps you could post your nginx.conf file here, and add commit messages to your github repo to show which files have been modified? In its current state, this is interesting but not immediately useful.

  2. Michael says:

    Hey, even if it’s a post from 2012, it was very useful to understand why it wasn’t usable like this.

    Do you know if the situation has evolved ? You’re almost the only one talking about this subject.

    I think that could be the best choice among plenty of others: nginx serving directly cached pages from memcached (without going through php), and w3 total cache storing and purging data.

Leave a Reply

Your email address will not be published. Required fields are marked *