If you’ve had problems with wp-super-cache not writing cached files into the cache/supercache/www.yoursite.com folder when running under nginx, then I think I’ve found the reason why…
There are actually two different problems here, but the first is the nginx-specific one and is a side-effect of your nginx configuration.
If you’re using rewrites so you have friendly URLs like /nginx-wp-super-cache-not-writing-cache-files-solved rather than index.php?p= and if like me you grabbed the nginx directives off the web somehere, you’ve probably got some lines like this:
if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
The last rewrite directive sets a querystring parameter called q with the friendly URL as its value. Deep in the guts of wp-super-cache there’s a test for any querystring parameters, and if there are, it won’t super-cache the page. Doh! So what to do?
Change line 250 (wp-cache-phase2.php in wp-super-cache version 0.9.4.3) from:
if( ! empty($_GET) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
to:
if( (count($_GET,1) > 1) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
So rather than checking for an empty querystring, we check for more than 1 querystring parameter, because due to our nginx config, we’ll always have one called q with a value of the current URL.
The second problem, and this applies regardless of whether you’re using nginx, apache or any other httpd, is cookies.
We all know that if you’re logged in to Wordpress, wp-super-cache won’t save or serve you super-cached files (it just uses the regular wp-cache files instead). But, during my digging, I also found out that even if you visit any areas of your site that require you to login (i.e. first redirect you to wp-login.php), and then you don’t login, you still will have been cookied with wordpress_test_cookie containing a value of WP Cookie check.
Again, due to the way wp-super-cache works, this will now be enough to stop it saving or serving you super-cached files, until you delete this cookie.
You can either remember to keep deleting this cookie, or you could just patch wp-cache-phase2.php as follows. Change lines 267-270 from:
if( $super_cache_enabled ) {
$user_info = wp_cache_get_cookies_values();
$do_cache = apply_filters( 'do_createsupercache', $user_info );
if( $user_info == '' || $do_cache === true ) {
to:
if( $super_cache_enabled ) {
$user_info = wp_cache_get_cookies_values();
if ($user_info == 'WP Cookie Check') $user_info = "";
$do_cache = apply_filters( 'do_createsupercache', $user_info );
if( $user_info == '' || $do_cache === true ) {
Comments
fixes
Thanks for the test cookie suggestion, just updated trunk with a check for that in wp_cache_get_cookies_values(). Grab the development version in 15 minutes to try it out.
Not sure about the other problem. Is there a variable that nginx sets so I can test for it reliably?
(Wish you had subscribe to comments installed, I just know I’ll forget to come back here to check for replies!)
Line 250 is different
You should specify that changing line 250 is in wp-cache-phase2.php. I had trouble finding it, so I turned to grep.
You are right
After I read the source code, I found the same cookie problem as you mentioned.
I can not understand why the writer have not meet such case.