NGINX default error page

alexandernst asked:

I’m trying to set a default error_page for my entire nginx server (as in for all vhosts).

I’m trying it with following code:

http {
    ...
    ...
    error_page 404 /var/www/default/404.html;
    ...
    ...
}

Also, I’d like to be able to create a vhost and make it use the default 404.html if I don’t explicitly write another one.

Something like:

server {
    ...
    server_name mydomain.com
    root /var/www/mydomain.com/;
    ...
}

Anyways, I’m getting the following error:

[error] 16842#0: *1 open() "/var/www/mydomain.com/var/www/default/404.html" failed (2: No such file or directory)

While I do understand the error, and I do understand why it’s happening, I can’t understand why can’t I tell NGINX to user the default error_page as an absolute path instead of appending it to the root of my vhost.

Any idea how to make it?

Regards

My answer:


As you’ve already discovered, the error_page directive specifies a document that is relative to the document root.

One way to work around this is to create a separate file containing your error page specifications, which contains the appropriate location blocks, and then include that from each server which will use the “global” error_page.

For example, a file /etc/nginx/global404:

location = /404.html {
    root /var/www/default;
}

error_page 404 /404.html;

Now in each server block, you will:

include global404;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.