How can I map error codes/pages in nginx to my more descriptive file names?


I have a dir with errors like 404-NotFound that I would like to easily map the 404 error code to in nginx. I do not want to have to specify each error_page mapping by hand as it would be tedious. It would also bloat the conf file because it would have to be done for each server.

I already have a map setup but new to nginx and can't figure out how to get it all to work together.

(All my error pages start with the error code so, in fact, I would rather have some way for the file to be automatically found since there is only one file per error code.

e.g., error 404 is mapped to file 404-NotFound.html and it would be nice if I didn't have to specify this but somehow it detected automatically.)

I would also like to support multiple languages...


error_page can contain variables in URI part but not in codes, so anyway it would require you to explicitly set that list. In decent Nginx versions (1.3.2, 1.2.2) variable $status appeared, allowing to approach your aim in a way like this:

error_page 404 403 … @err;

location @err {
    try_files /html/error/$status.html /html/error/default.html;

I have yet to test this, if you have the time to do it, try this:

  recursive_error_pages on;
  error_page 400 403 404 407 500 502 503 504 /path/to/$status-ErrorPage.html;

Good luck!

Edit: Did some research and found this, and now I just learned it's a BAD idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.