Using error_page to a named location - possible?

Ed W lists at wildgooses.com
Wed Mar 7 09:54:54 UTC 2012


On 06/03/2012 23:28, Maxim Dounin wrote:
> Hello!
>
> On Tue, Mar 06, 2012 at 10:32:56PM +0000, Ed W wrote:
>
>> On 06/03/2012 17:47, Maxim Dounin wrote:
>>>> 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;
>>>>    }
>>>>
>>>> location @502 {
>>>>    index /502.html;
>>>>    root /var/www;
>>>>    internal;
>>>> }
>>> Try something like:
>>>
>>>      location @502 {
>>>          internal;
>>>          root /var/www;
>>>          rewrite ^ /502.html break;
>>>      }
>>>
>>> This should work at least for GET requests.
>> Perfect - thanks!
>>
>>
>> Now, I have thought about this a little while and I don't quite
>> understand how it works...?
>>
>> The key seems to be the rewrite clause - near as I can see, without
>> this the error request gets reprocessed by the "location /" section,
>> but the rewrite is causing it to terminate that?
>>
>> Can you teach me to understand this please?
> Under normal conditions
>
>      location @502 {
>          root /var/www;
>      }
>
> will behave much like normal static location, as described here:
>
> http://nginx.org/en/docs/http/request_processing.html
>
> That is, request to a "/file", if it happens to hit his location,
> will be processed as follows:
>
> 1. It doesn't ends with '/', hence nginx will try to open a file
>     for it,<root>  +<uri>.  I.e. "/var/www/file" will be opened.
>
> 2. The (1) will likely fail (as there is no such file) and will
>     generate 404.
>
> More importantly, request to a "/" will be processed as
> follows:
>
> 1. It ends with "/", so nginx will try to use index module to
>     handle it.
>
> 2. Index module will test<root>  +<uri>  +<index>  file, i.e.
>     "/var/www/502.html" with your config.  If it's found - nginx will
>     do internal redirect to "/502.html".
>
> 3. The internal redirect will end up in "location /".  This is
>     obviously not what you want.
>
> With "rewrite ^ /502.html break;" the following two important
> things happen:

Hi, Thanks for this REALLY clear post.  I wonder if there is somewhere 
sensible to record this on the wiki - it's very incisive on a subtle 
subject!

Just to confirm my understanding, the point 2) above seems to the key - 
ie the "index" module is doing a "redirect" *without* a break, hence 
adding the explicit redirect WITH a break is what we need to prevent the 
cycle.  This fits with my observations (of course) because when I added 
a location matching "location /502.html" this also prevents the cycle

Thanks for investing quite some time answering this!


Note, the background to this is that I want to bounce any incoming url 
to "http://portal/login?redirect_to=$encoded_incoming_url".  I have 
tried to achieve this using only core nginx, but I can't handle the 
corner cases such as incoming url having multiple parameters.  I have 
worked around using my cgi to generate the redirect.  *IF* you believe 
that this should be possible using only nginx I would be interested to 
re-ask that question - however, I think you are going to confirm that 
it's not currently possible?

Many thanks again!

Ed W



More information about the nginx mailing list