WP Super Cache NGINX mobile theme

If you use NGINX, and want the best performance, you probably use a caching plugin such as WP Super cache with the standard rules over at wordpress.org.

Well, the biggest problem is that those rules don’t work if you use the Jetpack Mobile theme, or any other mobile theme. I figured out a way to fix that, and all it takes is editing your NGINX virtual host. Just replace the standard rules with:

#Browser Caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js){
        expires 30d;
location ~*\.(html){
        expires 7d;
# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.

set $cache_uri $request_uri;
set $file "index-https.html";
# 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';

#Set file to mobile if user agent
if ($http_user_agent ~* (2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800)) {
        set $file "index-https-mobile.html";
# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
    try_files /wp-content/cache/supercache/$http_host/$cache_uri/$file $uri $uri/ /index.php?$args ;

It’s basically just the standard rules, but instead of bypassing the cache for mobile devices, it serves the cached mobile version, if that option is enabled.

Why not just use PHP to serve cached files?

This is also what I did for a while, but using NGINX directly instead of invoking PHP is much faster, and uses less resources. Put those together, and your server can now deal with more visitors at a time, while being faster! Hopefully WP Super cache will copy W3 Total Cache, and just create a nginx.conf file. But, until then, you’ll have to edit the configs yourself.

%d bloggers like this: