Default error_page for multiple vhosts

Francis Daly francis at
Fri Feb 22 10:12:04 UTC 2013

On Fri, Feb 22, 2013 at 09:32:33AM +0100, Alexander Nestorov wrote:

Hi there,

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

Based on the way that nginx configuration inheritance works, that is
unlikely to be usefully possible at http{} level when you also want to
add an extra "error_page 502" to a few domains.

(That's not "it can't work"; it's "even when you add it at http{}
level, be aware that some later configuration changes may undo it in
some cases".)

> 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.

What's not to understand? error_page takes a uri, not a filename.

I guess that, up to now, no-one who wanted error_page to be able to
take a filename provided a patch with a convincing explanation of why
it would be useful to include.

> What I'm trying to achieve is being able to user
> /var/www/default/404.html error_page from all my vhosts without
> having to add it to all server{}. I think adding it for my 300 domains
> is an overkill.

I think that if you have 300 domains, then you should already have a way
to auto-generate the configuration. Just put the suggested configuration
into the template and regenerate.

(And if you *don't* have such a way, then you can probably do a one-off
thing with some text processing language; and you should probably
consider creating such a way, in order to make future "global" changes
easier for you.)

> I asked in serverfault and I did got a semi-solution using includes,
> but this solution still requires me to edit every
> single server{}, which I really don't want to.

Find each line "server {". Add one line with "include", or add a few
lines with the "error_page" and "location = /404 {}" configuration. Not
a lot of sed.

Run "nginx -t" to check for syntax errors.

If you're going to use nginx, it is likely to be more comfortable for
you if you adapt to its configuration method.

What you see as "overkill" and "semi-solution" and "don't want to",
I see as elegant and simple-to-read.

> Is there any other way I could achieve what I'm trying?

error_page which takes a filename -- not without patching.

Given that you want a common error_page which uses the same uri in each
server (which is ok), but you don't want to use per-server configuration
to map that uri to a common filename -- not that I can see.

Francis Daly        francis at

More information about the nginx mailing list