Nginx won’t serve an error_page in a subdirectory of the document root

custom-errorsnginx

Here's a snippet of my nginx configuration:

server {
    error_page 500 /errors/500.html;
}

When I cause a 500 in my application, Chrome just shows its default 500 page (Firefox and Safari show a blank page) rather than my custom error page.

I know the file exists because I can visit http://server/errors/500.html and I see the page. I can also move the file to the document root and change the configuration to this:

server {
    error_page 500 /500.html;
}

and nginx serves the page correctly, so it's doesn't seem like it's something else misconfigured on the server.

I've also tried:

server {
    error_page 500 $document_root/errors/500.html;
}

and:

server {
    error_page 500 http://$http_host/errors/500.html;
}

and:

server {
   error_page 500 /500.html;
   location = /500.html {
       root /path/to/errors/;
   }
}

with no luck.

Is this expected behavior? Do error pages have to exist at the document root, or am I missing something obvious?


Update 1: This also fails:

server {
    error_page 500 /foo.html;
}

when foo.html does indeed exist in the document root. It almost seems like something else is overwriting my configuration, but this block is the only place anywhere in /etc/nginx/* that references the error_page directive.

Is there any other place that could set nginx configuration?

Best Answer

Try adding

proxy_intercept_errors on;

or

fastcgi_intercept_errors on;

in server block depending of your configuration.

This sort of config is working ok:

  error_page   500 502 503 504  /500.html;
    location = /500.html {
        root   /home/static/pages;
    }