Using Nginx FastCGI Cache With WordPress

/ Misc, Wordpress / by Paul Robinson / 0 Comments

Are you sick of using different caching plugins? Are you sick of updating plugins and checking to make sure there are no updates to Nginx rules? Are you happy to get your hands a little dirty and write your own Nginx rules? Then this might just be the tutorial for you. Let’s take a look at how you can get your WordPress based website using Nginx FastCGI cache.

It should be noted that you still need a plugin, but this one is not to run the caching. This plugin is simply to allow you to conveniently clear the cache (and clear it after making posts etc) from the WordPress admin. I also expect that you have a somewhat decent knowledge of editing Virtual Host files used by Nignx, if you don’t I would suggest familiarising yourself with how to make Virtual Hosts first.

Install Nginx Extras

You most likely have Nginx installed already, but chances are high that it is from the default repo and that you don’t have the modules required to use the Nginx FastCGI cache. To solve this with the least disruption we can add the needed repo and grab Nginx Extras from it.

You’ll likely get a message asking you to resolve a conflict with your existing nginx.conf file. If you know you haven’t modified the nginx.conf file you can probably overwrite with the file from the repo. If you have made changes you will need to resolve the conflict. This most important thing is to make sure the following is in your nginx.conf file.

Once that is done you are ready to adjust your Virtual Host file. Some of this is completely depends on your website, but for the most part it should be fairly similar. I have removed anything to do with my SSL certificate, you should add you own back in or if you use Certbot just leave the parts it added for you intact.

I’ve changed paths etc to not reveal too much information. I encourage you to change things too. You can change the cache name and path to something more sensible. Don’t forget to change your the domain and path to your own if you decide to just copy/paste. You also want to make sure that the total memory used does not exhaust your available RAM. The amount of memory is defined in the first line after WORDPRESS.

Once you have added this you can test your configuration using:

If you get a message saying there were no errors, you are good to go. Restart or reload Nginx and you should be able to check if your page is being cached by using a incognito tab and checking the network tab of Chrome Dev tools (or FireFox Dev tools) for the X-FastCGI-Cache header. If it says ‘HIT’ after an initial page load to prime the cache then you are up and running.

Clearing The Cache

The best way too make it easy to clear the cache in WordPress is to install a small plugin. This will enable you to clear it manually or it will do so automatically after making a new post, commenting, etc. This plugin is called ‘Nginx Cache’ by Till Krüss and can be installed from your WordPress admin. Once installed head to the options page under the Tools menu and input the path you used for your cache.

Troubleshooting

It is by no means simple to get this cache up and running if you don’t deal with Nginx too often so it is fairly easy to hit problems. I didn’t come across anything in particular I can give you a heads up about, just to double check your spelling of things in your Nginx Virtual Hosts files, although the Nginx configuration test is great at letting you know if you got something wrong.

Another note is to always be careful. It is very easy to break your website completely, it might be a good idea to have a copy of your initial config so you can put it back in the case of any major issues.

As always if you have any problems please do let me know via the comments and I will try to help as best I can.

Leave a Reply

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

I'll keep your WordPress site up-to-date and working to its best.

Find out more