Strategies for using Nginx fastcgi_cache_purge with clustered servers for Wordpress

Love this site and services. fastcgi_cache_purge is a great feature and something crucial to a project I’m working on.

There is one caveat that I haven’t encountered however. I’m setting up a multinode wordpress website and was wondering if anyone had come across the problem of handling a cache purge request to multiple nodes initiated from one node.

For example, I have the following Nginx configuration to use with the nginx-helper plugin:

location ~ /purge(/.*) {
      fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}

Is there anyway to send that request (lua script?) to all nodes in my cluster simultaneously?

Hello, if want to purge any specific page example.com/hello, you can send http purge request to that page using url example.com/purge/hello from your master node to all your other nodes.

If you want to clear all Nginx cache from your node, we have php script called clean.php, keep that script in webroot of node and call using http example.com/clean.php

Also, if you are using EasyEngine then you can directly call to clean.php using following url: https://example.com:22222/cache/nginx/clean.php

fastcgi_cache is good for single-server setup.

For clusters, you may try memcache or some nginx’s redis module. In both cases, cache will be shared among multiple servers.

Only problem is - cache generation/priming need to handled on WordPress end.

We do not have any tutorial for this strategy but shared-cache, like memcache, will be less painful to manage.

Thanks for your suggestions.

I ended up writing a Wordpress plugin because I wanted to clear just the cache parts when a post or page has been updated.

This plugin code does send HEAD requests to other hosts (specified by the plugin in admin area) but the actual purging is untested. However, when I get it working correctly, I’ll push my changes to the following repo.

Thanks again!

That’s nice! :smile:

We were planning to add cluster fastcgi-cache-purge support to https://github.com/rtCamp/nginx-helper/ but all contributors of that plugins are busy or do not have need for that.

Once your plugin is ready/releases on wordpress.org, let us know.

You may send pull requests to https://github.com/rtCamp/nginx-helper/ which has many contributors.

Would be fabulous to work this into the the nginx-helper plugin actually. As of now, I’m hooking into http_api_debug and looking for purge URI’s.

I have a deployment deadline but perhaps I will write it in as a fork and send a pull request later on.

Hmmmm… The more I mess with this, the more I see your point with using some other cache mechanism. Of course, the best scenario is to be able to have this plugin handle cache purging per post. However, I have to differentiate cache also by host, whether it’s mobile client, or a mobile app! The issues aren’t the plugin, it’s the limitations of the nginx plugin. I may have to switch to microcaching. I really don’t want to use NFS, nor do I have the memory for memcached. Perhaps redis would be more appropriate (if it “swaps” to disk cache.) Not looking so feasible but I have one more trick trick to try…

May I know what do you mean by microcaching here?

For cache by host, mobile app, etc - you can append them to cache_key. So for every page multiple versions of cache gets maintained. You can decide cache key value in all cases, I guess. But yes, nginx fastcgi-cache wins here as it gives you ability to create cache at nginx-level.

Few more suggestions (sorry for just suggestions but I really have nothing more to offer at this point of time):

  1. If you can access other details in PHP, you may use/modify memcache wordpress plugin to store correct page version with correct key. Serving cached pages can be handled by nginx. You may use prefix as namespace to purge all versions of pages (from all servers - all servers part will be automatic as memcache can be shared across cluster).
  2. Similar approach with redis. May need more coding on WordPress as well as Nginx end.

If purging multiple versions is your only problem you may try using nginx-selective-cache-purge-module or bash script nginx-cache-inspector.

Also, nginx-plus has fastcgi-cache-purge by wildcard support.