5

I'm wondering if it's possible to have a global error_page directive in nginx without redirecting (changing the URL in the browser's address bar).

I know this is possible by using a named location, but global error pages are declared in the http block, which doesn't allow location directives. I have a number of vhosts in my configuration and I'm trying to avoid adding this to current and all future vhosts.

I'm currently using nginx 1.3.0 if that makes a difference.

nuceman
  • 51
  • 1
  • 3

1 Answers1

0

error_page directive is exactly what you need. It can be used not only on 'location' level but also at 'server' or 'http' levels.

kworr
  • 1,055
  • 8
  • 14
  • Thanks for the reply. error_page behaves differently when used in the http block than when used in a server or location block as it will do a full redirect to the error page URL. The only way to prevent this seems to be with the use of a named location, but you're not able to use location blocks inside the http block. Basically I'm attempting to set the error page globally for all vhosts on my server so that I don't have to explicitly set the error pages on all of them, cluttering up the configuration, but I don't want the browser to be redirected to the error_page URL. – nuceman Jun 18 '12 at 19:01
  • 1
    The only solution I've been able to come up with so far that's close to what I want has been to make a separate error.conf with the error page configuration and including it on all the vhosts. I'm assuming there is a way to do this with the error_page declarations in the http block but I have yet to come across a solution for it. – nuceman Jun 18 '12 at 19:09
  • You are right with your workarroud, works fine. After some googling I found this post: http://serverfault.com/questions/381375/nginx-can-i-configure-a-global-error-document Hope it helps someone – user9869932 Nov 18 '14 at 00:10
  • This answer does not answer the question. OP already mentions using the error_page directive but it makes a redirect. – qwertzguy Mar 03 '18 at 01:26
  • @qwertzguy: Please read documentation. This is internal redirect invisible to the end user. – kworr Mar 05 '18 at 09:20