đź”’ EXCLUSIVE: Wordpress nginx/tutorials/multisite/subdomains/wp super cache - Uncensored 2025

Topics

On this page

WP Super Cache + (optional) Domain-Mapping

đź”’ EXCLUSIVE: Wordpress nginx/tutorials/multisite/subdomains/wp super cache - Uncensored 2025

Update: This article is updated for WordPress 3.5 multisite’s file-handling.

easyengine (ee) note: If you are using easyengine, you can accomplish everything in this article using following command:
ee site create example.com --wpsubdom --wpsc
NGinx

In this article, we will setup WordPress Multisite using subdomains with Nginx with added support for WP Super Cache Plugin.

Nginx Config

Below is recommended Nginx configuration.

server {
        ##DM - uncomment following line for domain mapping  
        #listen 80 default_server;
    server_name example.com *.example.com ;
    ##DM - uncomment following line for domain mapping
    #server_name_in_redirect off;

    access_log   /var/log/nginx/example.com.access.log;
    error_log    /var/log/nginx/example.com.error.log;

    root /var/www/example.com/htdocs;
    index index.php;

    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';
    }   
    if ($query_string != "") {
        set $cache_uri 'NULL';
    }   

    # 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';
    }   

    # 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';
    }

    # Use cached or actual file if they exists, otherwise pass request to WordPress
    location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
    }    

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        #following line is needed by WP SUPER CACHE plugin
        fastcgi_param SERVER_NAME $http_host;
    }

    location ~* ^.+.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /. { deny  all; access_log off; log_not_found off; }
    location ^~ /wp-content/cache/ { deny  all; access_log off; log_not_found off; }    
}

Domain Mapping

You need to uncomment few lines in above nginx-config to get domain-mapping working. Apart from above config changes, you can read this guide to setup/configure domain-mapping.

Must Read:


Comments

  1. Gonçalo Peres Avatar
    Gonçalo Peres

    Great series the “WordPress Nginx Tutorials”. Well done and an interesting approach.

    But i have 3 questions.

    1) Why will “/index.php?$args” break WP Super Cache?
    I have a different setup for “Nginx + WordPress Multisite Subdomains + Domain Mapping Plugin + WP Super Cache”, similar to http://codex.wordpress.org/Nginx, and passing the $query_string to index.php doesn’t break WP Super Cache. It works all the same.

    2) Why is “fastcgi_param SERVER_NAME $http_host” essential for WP Super Cache?
    I don’t use it and it works all the same with the default setting in fastcgi.conf: “fastcgi_param SERVER_NAME $server_name”

    3) What is this block for “location ^~ /blogs.dir { … }”
    The request “/blogs.dir” shouldn’t exist, right? Why have a location for it? It should return 404.

    Maybe i’m wrong and you are right, but please explain. Thank you.

    Salutations.

    Gonçalo Peres

    1. Thanks Gonçalo. I will answer your questions 1-by-1 🙂 #1. Why will /index.php?$args break WP Super Cache? If you enable debugging logs for WP Super Cache, you will see Supercache caching disabled. Only using wp-cache. Non empty GET request. in error messages. ?$args will force WP Supercache to dropback to legacy mode in which it does low-performance caching. Ideally, you should be able to see a cached page, even if PHP crashes. Using /index.php?$args will throw bad-gateway error as PHP is used for caching (PHP-involvement degrades caching performance) I have published a checklist for perfect nginx setup. It has a simple test to verify if WP Super cache running to its full potential. #2. Why is “fastcgi_param SERVER_NAME $http_host” essential for WP Super Cache? WP Super Cache uses PHP’s