huskee 🌜︎

Easy custom error pages on nginx

posted Aug 08, 2023 by huskee

I wanted to have custom error pages for this website, but I didn't really want to write ~30-40 html files for the ~30-40 different http error codes. Thankfully, adriaan has thought of a solution on that, but I had to make some changes for it to fit my needs:

Snarky comments!!

Because I'm a sucker for a horrible pun, I decided I'd add comments to every status code, so that they display under the status code.

Unfortunately, nginx's map only supports 2 variables, so I had to get a bit hacky and create two of them: one containing variables $status and $status_text, and one containing $status_text and $desc, where I put my stupid jokes.

I ended up putting all of this into a file in /etc/nginx/conf.d, with an include directive in /etc/nginx/nginx.conf.

Then, I created a file in /etc/nginx/snippets called error_locations.conf, where I defined all the error_page directives and the single location directive needed for the single error page html file, as per adriaan's blogpost.

The result? Straight up tomfoolery.

Screenshot of a custom error page for HTTP status code 451 Payment Required Screenshot of a custom error page for HTTP status code 511 Network Authentication Required Screenshot of a custom error page for HTTP status code 421 Misdirected Request

(p.s. SSI I love you)