7

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

voretaq7
  • 79,879
  • 17
  • 130
  • 214
alexandernst
  • 534
  • 3
  • 9
  • 21

3 Answers3

10

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;
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • 3
    This indeed is a solution, but it's not perfect. Isn't there really some way I could tell NGINX to use a default error_page without having to put it on every server{} ? – alexandernst Feb 22 '13 at 07:56
  • Nope, sorry. This is about the best you can do. – Michael Hampton Feb 22 '13 at 08:01
  • Do you know if this was asked in the ML of NGINX? If it wasn't, I could try asking and see if they can implement it. – alexandernst Feb 22 '13 at 08:07
  • You can try. Maybe Igor will implement it, or maybe he will yell at you. :) – Michael Hampton Feb 22 '13 at 08:08
  • Hahaha, let's hope he'll do the first! Or at least won't do the second :P – alexandernst Feb 22 '13 at 08:08
  • @alexandernst Look in `nginx.conf`: if you see the following line in the `http` block: ```include /etc/nginx/conf.d/*.conf;``` then you may simply create a file `errors.conf` in `conf.d` and state your error_page directives in there. They will be applied to all of the server configurations in `http` – Aisteru Firë Jun 10 '23 at 09:40
1

Seems like error_page is inherited only if there's no other error_page directives in server/location. So to reset error_page 404 for your server, just set some other error page, like:

server {
  error_page 399 /;
}

This server won't inherit error_page 404 (and any other) from 'html' block.

Chupaka
  • 130
  • 4
0

I plan to use the include approach previously submitted but, for the sake of thoroughness, an alternative: Use symlinks. It still requires an extra operation for each vhost you manage but it does offer the benefit of not requiring further server config changes and restarts.

http {
    ...
    ...
    error_page 404 /error/404.html;
    ...
    ...
}

...and then in each vhost's root...

ln -s [path to error page html directory] /[vhost root]/error

This approach strikes me as inelegant and unportable (I'd vote it down if I have enough rep to vote things up or down) but it works.

Dave W.
  • 19
  • 5