Using error_page to a named location - possible?

Maxim Dounin mdounin at mdounin.ru
Tue Mar 6 18:24:06 UTC 2012


Hello!

On Tue, Mar 06, 2012 at 10:05:03PM +0400, Max wrote:

> 
> 06 марта 2012, 21:47 от Maxim Dounin <mdounin at mdounin.ru>:
> > Hello!
> > 
> > On Tue, Mar 06, 2012 at 04:37:41PM +0000, Ed W wrote:
> > 
> > > Hi, as part of a larger problem I have a captive portal which grabs
> > > all incoming URLs and after evaluating a bunch of logic redirects
> > > the caller somewhere else.  I use proxy_pass to pass the request to
> > > my cgi code.
> > >
> > > However, I would like to customise the error message in the event
> > > that the upstream daemon is unavailable.  I can't figure out how to
> > > do that affecting part of the incoming URLs.  Is it possible to do
> > > something like "error_page @502_error" in conjunction with
> > > proxy_pass?
> > 
> > Yes, see below.
> > 
> > >
> > > So, I have
> > >
> > > server {
> > >   listen 8000 default_server;
> > >
> > >   root /var/www/cp/htdocs/public;
> > >
> > >   location / {
> > >     proxy_read_timeout 5;
> > >     proxy_pass http://127.0.0.1:3000;
> > >     error_page 502 /502.html;
> > >     proxy_set_header Host $http_host;
> > >     proxy_set_header X-Server-Addr $server_addr;
> > >     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
> > >     proxy_set_header X-Forwarded-HTTPS 0;
> > >     proxy_set_header X-Captive-Portal-Redirect 1;
> > > }
> > >   location /502.html {
> > >     internal;
> > >   }
> > > }
> > >
> > >
> > > However, if I visit "http://somewhere/502.html" I get a 404 response
> > > (presumably matching the location /502.html and the 404 is due to
> > > the "internal"?)
> > 
> > Yes.
> > 
> > > I can't see that it's possible to use something like:
> > >
> > >   location / {
> > >     proxy_pass http://127.0.0.1:3000;
> > >     error_page 502 @502;
> > >   }
> 
> It is possible, you should use the proxy_intercept_errors directive:
> 
> location / {
>     proxy_pass http://127.0.0.1:3000;
>     proxy_intercept_errors on;
>     error_page 502 @502;
> }
> 
> location @502 {
>     # all named locations are internal by default, hence no "internal;"
>     return 502 "Gateway error while servicing $request_uri!\n";
> }

The proxy_intercept_errors isn't needed to handle 502's generated 
by nginx, it's only needed when backend returns the error in 
question and one want to override it.

Maxim Dounin



More information about the nginx mailing list