WP Super Cache: "try_files" aren't allowed in "if" blocks

Igor Clark lists at ruby-forum.com
Fri Sep 25 01:40:15 MSD 2009


Hi there, hope you're all well!

I'm trying to configure nginx for the Wordpress Super Cache plugin,
using the "Don't cache pages for logged in users" option.

I have the following config which works well once the cached file has
been generated:

    location ~ ^/my-blog(.*)$ {
        try_files $uri $uri/
/my-blog/wp-content/cache/supercache/$http_host/$uri/index.html
/my-blog/index.php;
    }

but I don't want to serve the cached file to logged-in users, which
according to the WPSC documentation can be found by checking the cookie
for "(comment_author_|wordpress|wp-postpass_)".

The following snippet I found on this list doesn't work with nginx
0.7.61 (and nothing I see in the 0.7.62 changelog appears relevant):

    location ~ ^/my-blog(.*)$ {
        if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_") {
            try_files $uri $uri/ /my-blog/index.php;
            break; # BTW, would this be necessary if it did work?
        }
        try_files $uri $uri/
/my-blog/wp-content/cache/supercache/$http_host/$uri/index.html
/my-blog/index.php;
    }

because apparently "try_files" is not allowed inside "if":

    [emerg]: "try_files" directive is not allowed here

So I tried this:

    location ~ ^/my-blog(.*)$ {
        if ($http_cookie !~* "comment_author_|wordpress|wp-postpass_") {
            rewrite ^/my-blog/(.*)$
/my-blog/wp-content/cache/supercache/$http_host/my-blog/$1/index.html
break;
        }
        try_files $uri $uri/ /my-blog/index.php;
    }

and that works fine once the cached file has been generated, but
obviously returns 404 beforehand.

I know I could just add

    if ($http_cookie !~* "comment_author_|wordpress|wp-postpass_") {
        if(-e [cached file]) {
            rewrite …;
        }
    }

but that would seem to defeat the object of using try_files to get away
from these if() constructions.

Is there a way to check cookie values more like a location test, or
even, or is there a better way to do what I'm trying to do?

Thanks for your help,
Igor Clark
-- 
Posted via http://www.ruby-forum.com/.





More information about the nginx mailing list