nginx giving of 404 when using set in an if-block

Posted by ba on Server Fault See other posts from Server Fault or by ba
Published on 2010-05-12T18:01:22Z Indexed on 2010/05/12 18:05 UTC
Read the original article Hit count: 237

Filed under:

I've just started using nginx and I'm now trying to make it play nice with the Wordpress plugin WP-SuperCache which adds static files of my blog posts.

To serve the static file I need to make sure that some cookies aren't set, that it's not a POST-request and making sure the cached/static file exist.

I found this guide and it seems like a good fit.

But I've noticed that as soon as I try to set something inside an if my site starta giving 404s on an URL that isn't rewritten.

The location block of the configuration:

location /blog {
    index index.php;

    set $supercache_file '';
    set $supercache_ok 1;

    if ($request_method = POST) {
        set $supercache_ok 0;
    }

    if ($http_cookie ~* "(comment_author_|wordpress|wp-postpass_)") {
        set $supercache_ok '0';
    }

    if ($supercache_ok = '1') {
        set $supercache_file '$document_root/blog/wp-content/cache/supercache/$http_host/$1/index.html.gz';
    }

    if (-f $supercache_file) {
        rewrite ^(.*)$ $supercache_file break;
    }

    try_files $uri $uri/ @wordpress;
}

The above doesn't work, and if I remove all the ifs above and add

if ($http_host = 'mydomain.tld') {
    set $supercache_ok = 1;
}

and then I get the exact same message in the errors.log. Namely:

2010/05/12 19:53:39 [error] 15977#0: *84 "/home/ba/www/domain.tld/blog/2010/05/blogpost/index.php" is not found (2: No such file or directory), client: <ip>, server: domain.tld, request: "GET /blog/2010/05/blogpost/ HTTP/1.1", host: "domain.tld", referrer: "http://domain.tld/blog/"

Remove the if and everything works as it should. I'm stymied, no idea at all where I should start searching. =/

ba@cell: ~> nginx -v
nginx version: nginx/0.7.65

© Server Fault or respective owner

Related posts about nginx