Welcome! Log In Create A New Profile

Advanced

Re: error_page

Maxim Dounin
October 20, 2010 01:36PM
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@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

_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

error_page

witekfl Gazeta.pl 2011 October 20, 2010 11:12AM

Re: error_page

Igor Sysoev 862 October 20, 2010 11:22AM

Re: error_page

Anonymous User 809 October 20, 2010 12:00PM

Re: error_page

Maxim Dounin 781 October 20, 2010 12:26PM

Re: error_page

Witold Filipczyk 773 October 20, 2010 01:08PM

Re: error_page

Maxim Dounin 793 October 20, 2010 01:36PM

Re: error_page

Igor Sysoev 876 October 20, 2010 01:42PM

Re: error_page

Maxim Dounin 831 October 20, 2010 01:50PM

Re: error_page

Igor Sysoev 933 October 20, 2010 01:58PM

Re: error_page

Igor Sysoev 892 October 20, 2010 01:06PM

Re: error_page

Witold Filipczyk 805 October 20, 2010 01:28PM

Re: error_page

Igor Sysoev 925 October 20, 2010 01:38PM

Re: error_page

Witold Filipczyk 1148 October 20, 2010 01:58PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 83
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready