Jay Caines-Gooby

Back in 5 mins

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

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!)

Gravatar icon
Friday May 29, 2009 @ 08:44 AM (BST)

You should specify that changing line 250 is in wp-cache-phase2.php. I had trouble finding it, so I turned to grep.

Gravatar icon
Thursday July 09, 2009 @ 12:20 AM (BST)

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.

Gravatar icon
mafai
Wednesday January 13, 2010 @ 03:23 AM (GMT)

Just add this line to your wp-config file

define('TEST_COOKIE', 'wp_test_cookie');

This renames the test cookie (from wordpress_test_cookie to wp_test_cookie) so it doesn’t trigger super cache to think you’re logged in.

With all good wishes,

Kevin.

Gravatar icon
Tuesday May 04, 2010 @ 05:58 AM (BST)

I had this problem for a long time, thank you all for the great tips, now it works here for me

Gravatar icon
Saturday February 12, 2011 @ 06:10 PM (GMT)
New comment

required, won't be displayed

optional

Don't type anything here unless you're an evil robot:


And especially don't type anything here:

Basic XHTML (including links) is allowed, just don't try anything fishy. Your comment will be auto-formatted unless you use your own <p> tags for formatting. You're also welcome to use Textile.

Copyright © 2011 Jay Caines-Gooby. All rights reserved.
Powered by Thoth.