fastcgi_cache + conditional purging + (optional) Domain-Mapping

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 --wpsubdom --wpfc

This article is a direct extension of yesterday’s article: WordPress + Nginx + fastcgi_cache with fastcgi_cache_purge module

Please read this to check if your Nginx is complied withfastcgi_cache_purge module, installing Nginx Helper plugin for your WordPress and few more important things.

WordPress-Multisite + Subdomains/Domain-Mapping + Nginx + fastcgi_cache with fastcgi_cache_purge module

Make changes to /etc/nginx/sites-available/ file so it looks like one below:

#move next 3 lines to /etc/nginx/nginx.conf if you want to use fastcgi_cache across many sites 
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

server {
    #@DM - uncomment following line for domain mapping or you will need to add every mapped-domain to server_name list 
    #listen 80 default_server;
    server_name * ;
    #@DM - uncomment following line for domain mapping
    #server_name_in_redirect off;

    access_log   /var/log/nginx/;
    error_log    /var/log/nginx/;

    root /var/www/;
    index index.php;

    #fastcgi_cache start
    set $skip_cache 0;

    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    if ($query_string != "") {
        set $skip_cache 1;

    # 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 $skip_cache 1;

    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;

    location / {
        try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        try_files $uri =404; 
        include fastcgi_params;

        fastcgi_cache_bypass $skip_cache;
            fastcgi_no_cache $skip_cache;

        fastcgi_cache WORDPRESS;
        fastcgi_cache_valid  60m;

    location ~ /purge(/.*) {
        fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";

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

The line fastcgi_cache_use_stale is what makes caching on Nginx-side unique. This line tells nginx to use old (stale) cached version of page if PHP crashes. This is something not possible with WordPress caching plugins.

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:

22 responses to “fastcgi_cache + conditional purging + (optional) Domain-Mapping”

  1. i had to remove the last line since it gives a lot of forbidden pages, the “location ~ /. { deny all”

    one more question, i enabled cache and timestamp in plugin but i dont see the lines in source, what could it be?

  2. Hi,

    i run the php-fpm-pool as a different user than nginx (“wordpress” and “www-data”).
    Is it possible that the cache-purge (via plugin) does not work?
    When i click on Purge-all-urls, the plugin-log states that all files are gone, but the files in /var/run/nginx-cache/*/* are still there and i still get the same cached site.

    regards and thanks for your work!

      • Sorry. Looks like you are referring to button added by a contributor from discussion here:

        It will *try* to purge-all but will eventually miss few things. Also even if ends up cleaning all “files”, after purge-all function gets called, you may continue to see folder like 0, 1, 2, 3 etc in nginx-cache dir.

        Finally, if you come across a case where you “expect” some URL’s cache to be cleaned but it does not happen, please let us know. We have tried to consider almost all cases so that even though 100% purge-all is not possible (without security issues) we will atleast get cache flushed, whenever we expect. (e.g. a page is edited, deleted, etc)

        In any case, php-fpm user/group won’t affect cache read/write. Its nginx alone who create and delete pages from cache. PHP only tells what to delete based on nginx-cache purging settings in plugin.

  3. I’ve copied this config and modified paths to point to the correct directories. When I try to start nginx, I get ‘unknown directive ” “‘ referring to the line number for “map $http_host $blogid {” What am I missing? It’s referring to whitespace!

    Yes, I have nginx-helper installed and all the boxes checked on WP. The map.conf has plenty of entries.

    • @Mackenzie

      Sorry for delayed reply. If your problem is not solved by now, can you please paste your complete nginx config at a service like:

      I will go through it. Most likely it means some line above “map” line is missing closing brace “}”.

  4. Rahul

    Hello, I am using Yoasts seo plugin with this configuration and I am having a rewrite issue with sitemap functionality. Nginx isnt stripping the www from from the initial domain or blog id=1. I have seen that you contributed to the plugins support page and hoping you could shed some light on this for me. The other mapped domains on the network redirect properly.
    Thank you for this great series of tutorials.
    best regards

    • Striping of WWW issue might be related to your Nginx config.

      For sitemap’s please add following codes inside server { } block:

      rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
      rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
  5. Hi, firstly, thanks so much for these great tutorials!

    I am having a little trouble though. I followed you tutorials step by step, only changing the ‘’ to my domain name. I have the nginx helper plugin installed and it looks like the whole site and everything works great. (It’s a network of about 40 sites moved from a LAMP setup)

    The only issue i’m having is that even with the setup on this page, using the fastcgi_cache, my page load time is extremely high.
    This is what it reads when I view the source code timestamp:

    Cached using Nginx-Helper on 2013-03-12 00:21:19. It took 62 queries executed in 13.000 seconds.
    Visit for more details

    Help with this problem would be highly appreciated! 🙂

  6. Thank from France for this great tutorial serie, certainly the best i have seen.
    very instructive.

  7. bad: location ~ /. { deny all; access_log off; log_not_found off; }
    good: location ~ /\. { deny all; access_log off; log_not_found off; }

    this rule denies access to hidden files (those starting with a .)

  8. YOU GUYS ARE AWESOME!!! It looks like you guys have the most rockin nginx wordpress plug and resources!

    I’m an old apache guy, swimming out into the new nginx water and likin’ what I’m seein’.

    I’ve just set up a MU using subdomains and went to use your plugin and found that my nginx doesn’t have fastcgi_cache_purge.

    I’m using Ubuntu 13.10 (saucy), and the brianmercer ppa doesn’t go up that high.

    I saw on another page you guys mention compiling it from source, and I’m game, but I’m wondering how much functionality of your wordpress plugin do I miss by not having Brian’s patch, and can I work around it?

    IOW, can I still use your plugin, what do I need to do different?

    *OR* can you recommend “the next best thing” until 14.04 comes out and (hopefully) Brian updates his ppa?

    Thanks again for all your great help!

      • BOOM!

        I followed that W3TC guide, and just ran the checklist for perfect wordpress-nginx setup, and my site is on top of it.

        (I’ve used W3TC on a couple of sites already, but not on nginx and didn’t realize it generates a nginx conf file to include in the nginx site config – you may want to consider putting that in your guide.)

        Wild thing, GTMetrix thinks a page load of 1.7sec (all from cache and amazon cloudfront) is 70%. Really? I’m guessing they want pages to load @ 0.5 seconds? But really, what human would think a 1.7 page-load is slow?

        Anyway – thanks again!

        • Nginx config I have posted already covers rules present in w3-total-cache’s nginx config file. Only in better way.

          About load time, It’s hard to achieve 0.5 seconds. Try combine+minify on CS/JSS. Remove external scripts like sharing buttons (you may try rtSocial for that )

          Real user load time depends on other files e.g. css, js, fonts, images so rather than load time, try to score A/A in page-speed and y-slow metrics.