Nginx Caching for WordPress: An Introduction and Tutorial
Table of Contents
If you want to speed up your WordPress site, you’ve almost certainly come across the concept of caching.
Nginx caching offers a different approach to caching than the WordPress caching plugins you might be familiar with, though.
In this post, we’ll dig into the topic of Nginx caching for WordPress, starting with an introduction to Nginx and the Nginx FastCGI cache module and finishing with a look at how to use Nginx caching with WordPress.
What is Nginx?
Before we can talk about Nginx caching for WordPress, it’s important to discuss what Nginx actually is.
Nginx is a popular web server, which is the underlying software that powers your WordPress site’s server.
Most cheap shared WordPress hosts use a different web server called Apache, but Nginx is popular with performance-focused managed WordPress hosts and is used by a majority of the world’s high-traffic websites.
According to W3Techs, Apache leads the overall web server market with 44% market share, while Nginx sits in second place at 30.7%. Nginx is a younger technology though and is rapidly expanding its market share.
Nginx offers a different asynchronous approach from Apache, where requests are handled in a single thread and one master process can control multiple worker processes. Unlike Apache, Nginx can handle thousands of HTTP connections at the same time, which can make Nginx a superior option for high-traffic/high-concurrency sites.
You don’t necessarily need to choose between Apache and Nginx, though. Some hosting stacks use Apache as the web server with Nginx in front acting as a reverse proxy.
How Does Nginx Caching for WordPress Work?
At a high-level, Nginx caching does something similar to caching plugins like WP Rocket.
When someone visits your WordPress site, your server needs to execute PHP and query your database to deliver the final HTML product to your visitors’ browsers. Caching works by storing that final HTML product and automatically delivering it to visitors, instead of dynamically rendering your content for each visit.
By eliminating that backend processing, caching can speed up your site and lessen the load on your server.
Nginx lets you do something similar via its fastcgi_cache module but at a server level. That is, Nginx is able to directly serve up your cached content without needing to involve PHP or WordPress.
In contrast, with a caching plugin, each request still needs to be handled by PHP at the application level. A caching plugin still eliminates much of the work and is definitely a positive for your site, but it also requires more CPU cycles than using Nginx to handle the request at a server level.
For example, Delicious Brains benchmarked Nginx FastCGI cache vs Varnish (another caching mechanism) vs the Simple Cache plugin at WordPress.org vs vanilla WordPress and found that Nginx FastCGI cache…
- Processed the most requests per second
- Had the lowest response time
Here are their results for the average response time (lower is better):
How to Use Nginx FastCGI Caching for WordPress
There are a few different ways that you can start using Nginx FastCGI caching with WordPress…
The Simplest Option
If you’re not a technical user, or just don’t want to deal with setting everything up, the simplest solution is to choose a WordPress host that supports Nginx caching out-of-the-box.
Many performance-focused WordPress hosts use the Nginx web server and/or use Nginx FastCGI cache. For example, both Kinsta and Flywheel use Nginx and its FastCGI cache module.
This gives you all the benefits of the Nginx FastCGI cache without requiring any technical knowledge.
The Middle Option
If you prefer something a little bit more flexible but don’t want to go all the way to setting up your own Nginx hosting stack, you can also use a tool like SpinupWP from Delicious Brains.
SpinupWP makes it really easy to host WordPress sites on your own DigitalOcean droplet and lets you enable Nginx FastCGI caching with the check of a box. From the SpinupWP dashboard, you can also set a custom cache duration and enter cache exclusion rules:
You’ll also be able to purge the Nginx FastCGI cache right from your WordPress dashboard.
The Advanced Option
If you’re more advanced, you can also set up your own server using Nginx and the Nginx FastCGI cache module. If you need help activating Nginx’s FastCGI cache module, you can follow this guide from DigitalOcean.
Then, to integrate Nginx FastCGI cache into WordPress, you can use the free Nginx Helper plugin at WordPress.org.
Nginx Helper has a few features to help your WordPress site work with Nginx, but the most relevant one here is the ability to purge the Nginx cache.
First, it adds a new Purge Entire Cache button that lets you manually purge the Nginx FastCGI cache as needed from your WordPress dashboard.
Second, it also lets you set up automatic purge rules. For example, you can automatically purge some or all of your content when you add, modify, or delete WordPress content:
In order to properly set up Nginx Helper with the Nginx FastCGI cache module, you’ll also need to make a few changes to your Nginx configuration files, which the plugin’s developer details here.
WP Rocket Works With Nginx and Nginx FastCGI Cache
WP Rocket offers out-of-the-box compatibility with Nginx, so you don’t need to do anything special to use WP Rocket with the Nginx web server.
To replace WP Rocket’s
.htaccess rules on NGINX, Maxime at SatelliteWP created a Rocket-Nginx configuration which you can find at GitHub and learn more about in this knowledgebase article.
You can use both Nginx FastCGI cache and WP Rocket’s page caching, though we don’t recommend this approach. However, if you decide to use them together and the Nginx FastCGI cache misses, WP Rocket can still serve up cached content at the application level.
If you use the Nginx Helper plugin mentioned above, you’ll be able to purge both the Nginx cache and the WP Rocket cache when you clear your cache.
By the numbers, Nginx is the second-most popular web server after Apache and is rapidly growing in popularity, especially with high-traffic sites.
Nginx excels in high-traffic situations and when serving static content. And you can either use only Nginx, or also put Nginx in front of Apache as a reverse proxy.
With the Nginx FastCGI cache module, you can serve static files directly from your server without needing to use PHP or hit your WordPress application, which is different than how most WordPress caching plugins function.
To use Nginx caching on WordPress, the simplest option is to choose a host that’s already configured everything for you. For example, Kinsta and Flywheel automatically use the Nginx FastCGI module for caching.
A good middle-ground is to use something like SpinupWP. SpinupWP lets you use your own DigitalOcean droplet, but gives you a convenient dashboard that lets you enable Nginx caching by checking a box.
Or, the most advanced option is to set up your own Nginx hosting stack. You can then use the free Nginx Helper WordPress plugin to automatically purge the cache according to rules that you choose.
Finally, WP Rocket offers out-of-the-box compatibility with Nginx servers, and you can also use the Rocket-Nginx configuration from Maxime to let Nginx serve previously-cached files without calling WordPress or PHP.