conditional "Expires" header config bug

Maxim Dounin mdounin at mdounin.ru
Wed Dec 8 00:36:27 MSK 2010


Hello!

On Tue, Dec 07, 2010 at 01:09:54PM -0800, ghazel at gmail.com wrote:

> Hi,
> 
> I'm trying to conditionally set the "Expires" header. This tiny
> snippet does not work as expected:
> 
>     location ~* testing/.*\.png$ {
>       if ($request_uri ~* "\.png\?[0-9]+$") {
>         expires max;
>       }
>       try_files $uri /testing/oops.png;
>     }
> 
> The basic problem is that if the png does not exist and there is a
> query string, I get a 404 instead of oops.png. All other combinations

This is expected - try_files isn't inherited into implicit 
location created by "if".

> seem to work fine. I have read IfIsEvil, but the alternatives
> presented (try_files, return, redirect) do not cover the case of
> setting headers.

Not "redirect" but "rewrite ... last".  Anyway, they are cover the 
case in question.

Generally safe parttern using only "return" looks like the 
following:

    location ~ \.png$ {
        recursive_error_pages on;
        error_page 418 = @expires;

        if ($args ~ "^[0-9]+$") {
            return 418;
        }

        try_files $uri /testing/oops.png;
    }

    location @expires {
        expires max;
        try_files $uri /testing/oops.png;
    }

    location = /testing/oops.png {
        # we don't want expires max on oops
    }

Maxim Dounin



More information about the nginx mailing list