return 404 from map module default value

Igor Sysoev igor at sysoev.ru
Fri Aug 5 13:33:11 UTC 2011


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;
>                 }
>         }

First, you do not need regexex here:

-         location ~* ^/404$ {
+         location  =  /404  {

-         location ~* ^/.+ {
+         location     /   {

The configuraiton should look like this:

http {

    map $uri  $redirected_uri {
        default  "";
        include  /etc/nginx/redirected_uri.txt;
    }

    server {
        location / {
            try_files  $uri  @redirect;
        }

        location @redirect {
            if ($redirected_uri) {
                return  301  $redirected_uri;
            }

            return 404;
        }
    }
}


-- 
Igor Sysoev



More information about the nginx mailing list