How-To Setup WP Super Cache Expert Caching On Nginx
In our article on setting up anĀ optimized WordPress blog, we briefly went through some of the basic setup options for WP Super Cache that any one, no matter what their server, would be able to run.
We are going to go even further and really push the plugin to the max for those that have the ability to edit server configuration files. This article assumes you have Linux as your operating system and are running Nginx as your web server as well as have access to your server through SSH. If you are not sure, please contact your host for help.
WP Super Cache Expert Caching with Nginx
For those running Nginx, we will need to edit a few of our server configuration files in order to use the Expert caching method that WP Super Cache provides.
The first thing we are going to do is locate where our configuration files are stored on our web server for Nginx. For me, I use CentminMod so my configuration files are located in /usr/local/nginx/conf/. Please adjust the below commands to take into consideration your file paths.
Create wordpress-super-cache.conf
We are going to create a new file in /usr/local/nginx/conf/ that will be called wordpress-super-cache.conf.
cd /usr/local/nginx/conf nano wordpress-super-cache.conf
Inside wordpress-super-cache.conf we are going to place the following:
# WP Super Cache rules. # Designed to be included from a 'wordpress-ms-...' configuration file. set $cache_uri $request_uri; # POST requests and urls with a query string should always go to PHP if ($request_method = POST) { set $cache_uri 'null cache'; } if ($query_string != "") { set $cache_uri 'null cache'; } # Don't cache uris containing the following segments if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { set $cache_uri 'null cache'; } # Don't use the cache for logged in users or recent commenters if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { set $cache_uri 'null cache'; }
Now save and exit the file.
Edit Your Domains Virtual Host File
Your next step is to locate your domains virtual host file. Since I am using CentminMod, my domain virtual host files are stored in /usr/local/nginx/conf/conf.d/ and we will use this site as an example so the file will be called myhelpfulguides.com.conf.
Since we are already in the conf directory we only need to go one directory up.
nano conf.d/myhelpfulguides.com.conf
Once inside your virtual host file you will need to place the following line directly above your first location entry.
include /usr/local/nginx/conf/wordpress-super-cache.conf;
then modify your try_files entry inside your location so that it now reads the following:
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args;
Your configuration should look something similar to this:
server { listen 443 ssl; listen [::]:443 ssl; ssl_certificate /usr/local/nginx/conf/ssl/myhelpfulguides.com.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/myhelpfulguides.com.key; ssl_client_certificate /usr/local/nginx/conf/ssl/cloudflare.crt; ssl_verify_client on; server_name www.myhelpfulguides.com; access_log /home/nginx/domains/myhelpfulguides.com/log/access.log combined buffer=256k flush=5m; error_log /home/nginx/domains/myhelpfulguides.com/log/error.log; include /usr/local/nginx/conf/autoprotect/myhelpfulguides.com/autoprotect-myhelpfulguides.com.conf; root /home/nginx/domains/myhelpfulguides.com/public; include /usr/local/nginx/conf/503include-main.conf; include /usr/local/nginx/conf/wordpress-super-cache.conf; location / { include /usr/local/nginx/conf/503include-only.conf; try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args; } include /usr/local/nginx/conf/staticfiles.conf; include /usr/local/nginx/conf/php.conf; include /usr/local/nginx/conf/drop.conf; #include /usr/local/nginx/conf/errorpage.conf; include /usr/local/nginx/conf/vts_server.conf; }
Now you can test that your configuration files are correct by using:
nginx -t
If everything checks out, you can now restart your nginx web server and head over to your WP Super Cache Settings and Advanced tab then enable the Expert caching setting.
It is probably wise to also delete the cache so that WP Super Cache can start fresh.
You will see that it is now working if you see your files being cached underneath WP-Super-Cache instead of WP-Cache like below: