WordPress Multisite using subdirectories, with W3 Total Cache


#1

Hey there,

I recently switched from a subdomain to subdirectory install, and can’t seem to get the minification working on the sub blog (example.com/site2/). Everything else works perfectly fine on the main site. Here’s my Nginx config:

server {  
	listen 80;  
	server_name www.example.com;   
	return	301 http://example.com$request_uri;  
}  

server {  
	listen 80;  
	  
	root /var/www/example.com/public_html;  
	index index.php index.html index.htm;  
	  
	server_name example.com *.example.com;  
	  
	##					    
	# Access and error log locations  
	##					    
	  
	access_log off;  
	error_log /var/log/nginx/example.com.error.log;  
	  
	##							        
	# Global Configurations/Restrictions  
	##  
	  
	# Global PHP include  
	include conf.d/global/php.conf;  
	  
	# Global restrictions  
	include conf.d/global/restrictions.conf;  
	  
	# W3 Total Cache  
	include conf.d/w3totalcache/example.com.conf;  
	  
	# WordPress multisite subdirectory  
	if (!-e $request_filename) {  
		rewrite /wp-admin$ $scheme://$host$uri/ permanent;	  
		rewrite ^(/[^/]+)?(/wp-.*) $2 last;		  
		rewrite ^(/[^/]+)?(/.*\.php) $2 last;                     
	}  
}

The includes, to keep things neat:

include conf.d/global/php.conf

location / {  
	try_files $uri $uri/ /index.php?q=$uri&$args;  
}  

location ~ \.php$ {  
	try_files $uri =404;  
	fastcgi_pass unix:/var/run/php5-fpm.socket;  
	fastcgi_index index.php;  
	include fastcgi_params;  
}  

# Allow cross origin fonts  
location ~* \.(eot|otf|ttf|woff)$ {  
    add_header Access-Control-Allow-Origin *;  
}

include conf.d/global/restrictions.conf;

location = /favicon.ico {  
	log_not_found off;  
	access_log off;  
}  

location = /robots.txt {  
	allow all;  
	log_not_found off;  
	access_log off;  
}  

location ~ /\. {  
	deny all;  
}  

location ~* /(?:uploads|files)/.*\.php$ {  
	deny all;  
}

And the last would be the one that W3TC writes to, which works fine.

Which rules am I missing to enable the minification of CSS/JS in example.com/site2/?
When using the subdomain variation, the above rules were the same except for the rewrites, I believe, and things kind of worked. It minified and output the files, but also didn’t remove copies of the theme’s assets, so everything was duplicated. But that’s not the issue here, sorry.

I’ve tried using your tutorial here, but changed back because I had the same issue: https://rtcamp.com/wordpress-nginx/tutorials/multisite/subdirectories/w3-total-cache/

Thanks for any feedback you might be able to provide.


#2

Please paste your nginx rules for w3 total cache

# W3 Total Cache  
include conf.d/w3totalcache/example.com.conf;

#3

Sure thing. Here it is:

# BEGIN W3TC Minify cache  
location ~ /assets/cache/minify.*\.js$ {  
    types {}  
    default_type application/x-javascript;  
    expires modified 31536000s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
}  
location ~ /assets/cache/minify.*\.css$ {  
    types {}  
    default_type text/css;  
    expires modified 31536000s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
}  
location ~ /assets/cache/minify.*js\.gzip$ {  
    gzip off;  
    types {}  
    default_type application/x-javascript;  
    expires modified 31536000s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
    add_header Content-Encoding gzip;  
}  
location ~ /assets/cache/minify.*css\.gzip$ {  
    gzip off;  
    types {}  
    default_type text/css;  
    expires modified 31536000s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
    add_header Content-Encoding gzip;  
}  
# END W3TC Minify cache  
# BEGIN W3TC Page Cache cache  
location ~ /assets/cache/page_enhanced.*html$ {  
    expires modified 3600s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding, Cookie";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=3600, public";  
}  
location ~ /assets/cache/page_enhanced.*gzip$ {  
    gzip off;  
    types {}  
    default_type text/html;  
    expires modified 3600s;  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
    add_header Vary "Accept-Encoding, Cookie";  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=3600, public";  
    add_header Content-Encoding gzip;  
}  
# END W3TC Page Cache cache  
# BEGIN W3TC Browser Cache  
gzip on;  
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;  
location ~ \.(css|htc|less|js|js2|js3|js4)$ {  
    expires 31536000s;  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
}  
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {  
    expires 3600s;  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=3600, public";  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
}  
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ {  
    expires 31536000s;  
    add_header Pragma "public";  
    add_header Cache-Control "max-age=31536000, public";  
    add_header X-Powered-By "W3 Total Cache/0.9.4";  
}  
# END W3TC Browser Cache  
# BEGIN W3TC Minify core  
rewrite ^/assets/cache/minify.*/w3tc_rewrite_test$ /assets/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;  
set $w3tc_enc "";  
if ($http_accept_encoding ~ gzip) {  
    set $w3tc_enc .gzip;  
}  
if (-f $request_filename$w3tc_enc) {  
    rewrite (.*) $1$w3tc_enc break;  
}  
rewrite ^/assets/cache/minify/(.+/[X]+\.css)$ /assets/plugins/w3-total-cache/pub/minify.php?test_file=$1 last;  
rewrite ^/assets/cache/minify/(.+\.(css|js))$ /assets/plugins/w3-total-cache/pub/minify.php?file=$1 last;  
# END W3TC Minify core  
# BEGIN W3TC Page Cache core  
set $w3tc_rewrite 1;  
if ($request_method = POST) {  
    set $w3tc_rewrite 0;  
}  
if ($query_string != "") {  
    set $w3tc_rewrite 0;  
}  
if ($request_uri !~ \/$) {  
    set $w3tc_rewrite 0;  
}  
if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {  
    set $w3tc_rewrite 0;  
}  
if ($http_cookie ~* "(w3tc_preview)") {  
    set $w3tc_rewrite _preview;  
}  
set $w3tc_enc "";  
if ($http_accept_encoding ~ gzip) {  
    set $w3tc_enc _gzip;  
}  
if (!-f "$document_root/assets/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc") {  
  set $w3tc_rewrite 0;  
}  
if ($w3tc_rewrite = 1) {  
    rewrite .* "/assets/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc" last;  
}  
# END W3TC Page Cache core  

(wp-content has been renamed to assets via wp-config. W3TC correctly applies it).


#4

Okay, so I added back everything from this config: https://rtcamp.com/wordpress-nginx/tutorials/single-site/w3-total-cache/

And things are almost working. The subsite loses its CSS and JS and adds the minified versions. The only problem is that it’s trying to pull from: example.com/site2/assets/cache/minify/000002/M9BPLkpMK0lN0c_JTNJPLi.css

Note the “/site2/” in there. Removing that in Firebug allows the CSS is be displayed correctly.

I believe it has something to do with this snip:

        location ~ ^/assets/cache/minify/[^/]+/(.*)$ {  
                try_files $uri /assets/plugins/w3-total-cache/pub/minify.php?file=$1;  
        }

Also, on an unrelated note: your favicon here on RTCamp is causing your SSL to display a warning.


#5

@Gshock
can you provide us access to your server so that we can look into details.
you can mail me on harshad.yeola@rtcamp.com


#6