Search code examples
nginxerror-handlingcustom-error-pagescustom-error-handling

nginx error location for all servers


Is it possible to define a common location for all servers? From nginx location documentation I've seen that location depends on server. I would like to do something like this:

...
http {
    error_page  404                    /error/404.html;
    error_page  500 501 502 503 504    /error/50x.html;

    location ^~ /error/ {
        internal;
        root /var/www/nginx/errors;
    }

    server {
        ...
    }

    server {
        ...
    }
    ...
}

I've tried setting:

http {
    ...
    root /var/www/nginx/errors; # also with root /var/www/nginx
    ...
}

with no success: always showing nginx default error page.


Solution

  • Is it possible to define a common location for all servers?

    No.

    You could make separate file and include it into all your servers.

    /etc/nginx/error-location.inc:

    location ^~ /error/ {
        internal;
        root /var/www/nginx/errors;
    }
    

    And then:

    server {
        ...
        include error-location.inc;
    }
    
    server {
        ...
        include error-location.inc;
    }