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

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 C.

Hello!

On Thu, Sep 24, 2009 at 11:40:15PM +0200, Igor C. wrote:

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

    try_files $uri $uri/ /my-blog/index.php;
    }
}

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?

Try something like this:

location /my-blog {
    if ($http_cookie ~ "comment_author_|wordpress|wp-postpass_") } {
        rewrite ^/my-blog(.*) /my-blog-loggedin$1 last;
    }

    # non logged in users

    try_files $uri $uri/
              /my-blog/wp-content/cache/supercache/$http_host/$uri/index.html
              /my-blog/index.php;
}

location /my-blog-loggedin {
    internal;
    rewrite ^/my-blog-loggedin(.*) /my-blog$1 break;
    try_files $uri $uri/ /my-blog/index.php;
}

Maxim D.

Maxim D. wrote:

Try something like this:

location /my-blog {
    if ($http_cookie ~ "comment_author_|wordpress|wp-postpass_") } {
        rewrite ^/my-blog(.*) /my-blog-loggedin$1 last;
    }

    # non logged in users

    try_files $uri $uri/
              /my-blog/wp-content/cache/supercache/$http_host/$uri/index.html
              /my-blog/index.php;
}

location /my-blog-loggedin {
    internal;
    rewrite ^/my-blog-loggedin(.*) /my-blog$1 break;
    try_files $uri $uri/ /my-blog/index.php;
}

Thank you Maxim, I think that’s what I’m looking for! Nice lateral
thinking.

Best
Igor