73 65 6C 65 6E 65 🌜︎︎(blog)

home :: projects :: blog? :: contact

easy custom error pages on nginx

posted Aug 08, 2023 04:30 UTC+03:00 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)