I created some custom 4xx and 5xx HTML error pages for most errors listed on the Wikipedia page. I'd like to install them on any nginx server I set up, without impacting performance too much.
Following the documentation I came up with this configuration directive for an error:
error_page 404 /nginx/404.html;
location = /nginx/404.html {
alias /usr/share/nginx/errors/404.html;
internal;
}
I put this in /etc/nginx/snippets/errors.conf
and can include the snippet in all my server
blocks.
This works great, however...
It prepends nearly 50 location
directives to every server
block on the box.
Is this excessive, and is there a more performance-friendly way to achieve the same thing? I avoided server-side includes, if statements, and rewrites thinking it would require more server resources.
When you don't map each error page to it's own location you can do with a single location directive:
Why not try Server Side Includes (
ssi on;
) in your error location? Then, you could define a single error page for all error codes. It's dynamic without involving a real programming language. Here's a good example of a template that illustrates most of what you can do with the technology: https://www.cambus.net/nginx-and-server-side-includes/