Search code examples
nginxcustom-error-pages

Nginx error page when error page does not exist


I would like to have custom error pages for each domain that is hosted on my server.

I have a directory structure like the following

data
   defaults
      error.html
   example.com
      misc
         error.html
   example.net
   ...

Now i would like to show misc/error.html if it is there for the domain name being accessed and else display default/error.html.

What i currently have what isn't working is the following. It doesn't work because it just displays the default nginx 404 page if the error page (misc/error.html) is not found (even when the error isn't even a 404 error).

    server_name ~^(www\.)?(?<domain>.+)$;

    location / {
        root /data/$domain;
        try_files $uri $uri/ =404;
        index index.html index.htm index.php;
    }

    error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;
    location @error {
        internal;
        ssi         on;
        auth_basic  off;
        root        /data;
        try_files   /$domain/misc/error.html /defaults/error.html;
    }

Solution

  • The very last try_files parameter is treated differently from all the others. If you read the documentation carefully you'll find it can be an HTTP error code, a named location ID or a new URI to re-evaluate, and your /defaults/error.html is treated as a new URI. Then nginx tries to serve it via the main location / { ... } searching the error.html file under the /data/$domain/defaults directory. And since the /data/$domain/defaults/error.html file does not exists, it returns a built-in 404 error page (it will not try the custom error page twice). Changing your try_files directive from the error handler to something like

    try_files /$domain/misc/error.html /defaults/error.html =404;
    

    should fix the situation.