error_page

Maxim Dounin mdounin at mdounin.ru
Wed Oct 20 21:35:20 MSD 2010


Hello!

On Wed, Oct 20, 2010 at 07:03:37PM +0200, Witold Filipczyk wrote:

> On Wed, Oct 20, 2010 at 08:25:42PM +0400, Maxim Dounin wrote:
> > Hello!
> > 
> > On Wed, Oct 20, 2010 at 05:58:09PM +0200, witekfl at gazeta.pl wrote:
> > 
> > > On Wed, Oct 20, 2010 at 07:21:19PM +0400, Igor Sysoev wrote:
> > > > On Wed, Oct 20, 2010 at 05:10:59PM +0200, witekfl Gazeta.pl wrote:
> > > > 
> > > > > Hi,
> > > > > probably better place for these questions would be nginx-users, but I ask
> > > > > here.
> > > > > I want to serve static pages as error codes but without changing the error
> > > > > code.
> > > > > 
> > > > > Now if:
> > > > > error_page 404 http://blablabla.com/404.html
> > > > > There is 302 and next 200 OK
> > > > > I want 404.
> > > > > 
> > > > > error_page 404 /404.html
> > > > > it works, but:
> > > > > if there is:
> > > > > location = /404.html {
> > > > > internal;
> > > > > }
> > > > > then cannot be a normal location
> > > > > location = /404.html {
> > > > > },
> > > > > so request such as http://blabla1.com/404.html won't work.
> > > > > 
> > > > > error_page 404 @404;
> > > > > location @404 {
> > > > >    root /blabla;
> > > > >    index 404.html;
> > > > > }
> > > > > doesn't work either
> > > > > 
> > > > > There must be some way to achieve it.
> > > > > How?
> > > > 
> > > > I do not understand the problem. With this configuration
> > > > 
> > > > error_page  404 /404.html;
> > > > locaiton = /404.html {
> > > >     internal;
> > > >     root /path/to/page;
> > > > }
> > > > 
> > > > you will get 404 code with /404.html body for both requests:
> > > > 
> > > > http://blabla1.com/non-existant.html
> > > > http://blabla1.com/404.html
> > > 
> > > Yes, but I want:
> > > http://blabla1.com/404.html to be 200 OK and serve http://blabla1.com/404.html
> > > not /path/to/page/404.html
> > 
> > Something like this:
> > 
> >     error_page 404 @notfound;
> > 
> >     location @notfound {
> >         rewrite ^ /404.html break;
> >         root /path/to/page;
> >     }
> > 
> > should do the trick.
> 
> Yes, it works. Thanks!
> Could you explain, why it works.
> Why this rewrite is required?
> Does it mean that for 404 by default /404.html is requested or not?

Short answer:

You have to use rewrite to map request to correct file.

Long answer:

Normally nginx uses error pages in the same URI namespace as the 
site itself (i.e. error_page 404 /404.html; will use the same file 
as available though normal /404.html request).  Note that 
"internal" doesn't introduce another namespace, it's only protects 
some part of the namespace from direct outside access.

If you want for some reason to introduce another namespace which 
won't interfere with site's one - you have to use named locations.

On the other hand, redirect to named location doesn't change URI, 
so request to "/something-not-here" with

    error_page 404 @notfound;

will result in request to "/something-not-here" being processed in 
location @notfound.  This will map to <root> + <uri> file, i.e. 
"/path/to/page/something-not-here".  To map request to correct 
file one have to use "rewrite".  With

    location @notfound {
        rewrite ^ /404.html break;
        root /path/to/page;
    }

any request in location @notfound will be mapped to 
"/path/to/page/404.html" file.

[...here should be explanation why "alias" won't work instead of 
root + rewrite, but I'm bored...]

Note well: I don't recommend this aproach as I consider having 
error pages in site's namespace is a good practice.  Additionally, 
this aproach is less efficient than normal error pages.  I've just 
outlined how to do what you're asked for.

Maxim Dounin



More information about the nginx-devel mailing list