return 404 from map module default value

Maxim Dounin mdounin at mdounin.ru
Fri Aug 5 13:29:01 UTC 2011


Hello!

On Fri, Aug 05, 2011 at 09:21:11AM -0400, zflairz wrote:

> I'm using map module to redirect URLs which have been removed (but
> indexed by search engine).
> But how can I fall back to http 404 response for urls not present in my
> redirect list?
> 
> I've tried following, but I got 200 instead of 404:
> 
>         location ~* ^/404$ {
>                 return 404;
>         }
> 
>         error_page 404 = /404.html;
> 
>         map $uri $redirected_uri {
>                 default /404; #here?
>                 include /etc/nginx/redirected_uri.txt;
>         }
> 
>         location ~* ^/.+ {
>                 if (!-f $request_filename) {
>                         rewrite ^ $redirected_uri permanent;
>                 }
>         }

You return 301 redirect here for all non-existing files.  If you 
want to return 404, try something like this instead:

    map $uri $redirected {
        default "";
        include /path/to/list;
    }

    error_page 404 = /404.html;

    location / {
        try_files $uri = @redirect;
    }

    location @redirect {
        if ($redirected) {
            rewrite ^ $redirected premanent;
        }

        return 404;
    }

Maxim Dounin



More information about the nginx mailing list