Search code examples
nginxerror-handlinghttp-status-code-404

One location block for all Nginx error codes


Is it possible to have the 40x and 50x errors served by a single location rule? Something like:

error_page 403 /403.html;
error_page 404 /404.html;
error_page 405 /405.html;
error_page 500 501 502 503 504 /5xx.html;

location ~ /(?:40[345]|5xx)[.]html$ {
    root /var/www/default/error;
}

Solution

  • error_page 403 /error/403.html;
    error_page 404 /error/404.html;
    error_page 405 /error/405.html;
    error_page 500 501 502 503 504 /error/5xx.html;
    
    location ^~ /error/ {
        internal;
        root /var/www/default;
    }