custom error pages for retun directive

Maxim Dounin mdounin at mdounin.ru
Tue Aug 26 11:59:43 UTC 2014


Hello!

On Tue, Aug 26, 2014 at 04:48:41AM -0400, kay wrote:

> It is not possible to set custom error page. For example
> /usr/share/nginx/404.html contains "test":
> 
> server {
> error_page 404 /404.html;
> 
> if ($request_method = "GET") 
> return 404;
> }
> 
> location / {
> proxy_pass http://localhost:8080;
> }
> 
> location /404.html {
> /usr/share/nginx/404.html;
> }
> }

[...]

> Is it possible to get cutsom 404 error page with 404 error code using
> "return 404" directive?

The request processing with the above configuration is as follows:

- if (...) matches, 404 returned;

- error_page redirects the request to /404.html;

- if (...) again matches, 404 returned;

- recursive_error_pages is off, so builtin 404 page is returned.

As you can see, the problem is that "if (...)" specified at 
server level matches again after the redirect.  To fix things, 
move the if into the "location /", it will prevent if from 
matching requests to /404.html:

    location / {
        if ($request_method = "GET") {
            return 404;
        }

        proxy_pass http://localhost:8080;
    }

    location = /404.html {
        # static file
    }

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list