Nginx rewrite: last & break

António P. P. Almeida appa at perusio.net
Mon Oct 24 23:58:12 UTC 2011


On 23 Out 2011 16h26 WEST, mdounin at mdounin.ru wrote:

Hello Maxim,

Can this be reasoned like this?

1. last means the *last* rewrite, after which the location matching
   phase reoccurs, in search of a content handler.

2. break means that the rewrite phase is done and we proceed to the
   next phase. There must be a content handler (be a location) for
   this to be true.

3. Outside a location block there's no content handler, hence using
   last or break is basically the same.

Thx,
--- appa

> Hello!
>
> On Sat, Oct 22, 2011 at 02:04:21PM +0300, Nginx User wrote:
>
>> On 22 October 2011 06:30, Ryan Chan <ryanchan404 at gmail.com> wrote:
>>> Anyone would like to do a quick summary for the issue?
>>
>> Minaev's answer in the serverfault link you provided is the
>> clearest explanation I have seen to date of this.
>>
>> To paraphrase, he says, within a location block, when you use
>> "last" the rewrites are stopped and a new subrequest is generated
>> which will
>
> Just a side note: "subrequest" is incorrect term here.
>
>> take all all locations into account. When you use "break" the
>> rewrites are stopped and processing continued within the location
>> you are in.
>>
>> Adding Maxim's statement above into account, outside a location
>> block, "break" behaves just like "last" does since there are no
>> location directives to run here.
>>
>> Someone just needs to update the docs if this is correct.
>
> Reading rewrite module docs carefully enough will tell basically 
> the same:
>
> ...
> If the URI changed as a result of the execution of directives 
> inside location, then location is again determined for the new 
> URI.  This cycle can be repeated up to 10 times, after which Nginx 
> returns a 500 error.
> ...
>
> ...
> Example:
>
> rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  last;
> rewrite  ^(/download/.*)/audio/(.*)\..*$  $1/mp3/$2.ra   last;
> return   403;
>
> But if we place these directives in location /download/, then it 
> is necessary to replace flag "last" by "break", otherwise Nginx 
> will hit the 10 cycle limit and return error 500:
>
> location /download/ {
> rewrite  ^(/download/.*)/media/(.*)\..*$  $1/mp3/$2.mp3  break;
> rewrite  ^(/download/.*)/audio/(.*)\..*$  $1/mp3/$2.ra   break;
> return   403;
> }
> ...
>
> The translation though looks awful and needs re-translation; 
> additionally, it was already polluted with user comments, and this 
> doesn't improve readability either.
>
> Maxim Dounin
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx



More information about the nginx mailing list