0

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.

NoChecksum
  • 125
  • 1
  • 8

2 Answers2

2

When you don't map each error page to it's own location you can do with a single location directive:

error_page 402 /error/402.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html; 
... etc. 

location = /error/ {
        alias /usr/share/nginx/errors/;
}
HBruijn
  • 72,524
  • 21
  • 127
  • 192
0

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/