That is if you’re still doing subdirectories. In most cases WordPress Multisite/Network will do the job properly without having to fiddle with nginx configurations files and whatnot.

Here we go. W3TC generates an nginx.conf file in the root of your installation and asks you to include it. You’ll need to stop doing this as we’ll make changes to some of the important directives in it and W3TC will overwrite them.

But since some of the directives can change you’ll need to follow these simple rules in order to make them compatible with WordPress subdirectory installs.

First, some preparations. In your server block add the following directive(s) as needed:

set $subsite ""; # main site
if ($request_uri ~* "^/subsite1/.*") {
    set $subsite "subsite1";
if ($request_uri ~* "^/subsite2/.*") {
    set $subsite "subsite2";
# and so on...

This will make a very crucial $subsite variable available that we need to inject into some of the W3TC nginx configuration directives.

Great. Next copy the proposed nginx.conf file that W3TC generated and include it in your server block. And make the following important changes:

1. Search for $document_root and append /$subsite to it. This will enable nginx to look in the correct wp-content/cache/ directories based on which site is being visited.

2. Find a line that contains rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_ssl$w3tc_preview.html$w3tc_enc" last; and change /wp-content/cache to /$subsite/wp-content/cache. Similar to the previous rule, this is needed to make sure the correct cache file is picked.

3. For each and every location directive that looks in /wp-content/ set it to .*/wp-content/. This will match and activate the needed directives for each and every subsite as needed.

That’s pretty much it. Those are the basic rules and the overall principle – modify the paths for a corresponding installation.

P.S. W3TC is great and all. But I always recommend something more minimal. Check out Pressjitsu Page Cache for Redis if you’re using Redis already. It’s pretty cool. Full disclosure: I wrote it 🙂