How to Optimize Wordpress Performance with nginx and WP Super Cache
This is a simple and effective method how to serve Wordpress pages blazingly fast: produce static HTML files with WP Super Cache, and serve them directly with nginx.
WP Super Cache (on Github) is an immensely popular, official Wordpress caching plugin with more than 1 million active installations. Basically the plugin produces static html pages of your posts and pages, and anonymous users can directly load the html without any interaction with PHP.
nginx is a very fast, flexible webserver, reverse proxy, load balancer and cache. If you are using Apache, WP Super Cache explains mod_rewrite rules to achieve the same thing as explained in this post.
After installing the WP Super Cache plugin, enable caching in the plugin settings, and configure the garbage collector timeout according to your needs.
Once enabled, visits by anonymous users (user which are not logged in) will create static .html files in the /wp-content/cache/supercache/ directory.
To achieve optimal performance, serve those .html files directly, bypassing PHP altogether. The following nginx config snippet works well for http:// and https:// sites. I save this snippet as wp-supercache.conf and reference it from the various server configs.
Now just include the above snippet from a nginx server config:
As always, test and reload the nginx config with nginx -t && nginx -s reload. This command will only reload the config if it doesn’t contain any errors.
Now let’s test if it works:
The first anonymous visit will call the PHP code and produce the static .html file
The second anonymous visit will receive the cached html
How do we know it didn’t go through the PHP WP Super Cache? Because WP Super Cache adds it’s own header, as you can see in this example with the nginx config disabled:
If you run into problems, you can always enable the WP Super Cache debugging which logs a lot of info into a logfile, and manually delete the /wp-content/cache directory for a fresh start.
You can easily verify that WP Super Cache is producing the static files by showing all files in the directory wp-content/cache, for instance with the command tree or find ./, or by opening the “Contents” tab in the WP Super Cache Settings and then clicking “Regenerate cache stats”.
And that’s it! Enjoy your blazingly fast Wordpress setup 😎🚀
If you have suggestions or feedback, let me know via @metachris.