I don't understand the difference between break and last (flags of rewrite). The documentation is rather abstruse. I've tried to switch between the two in some of my configs, but I couldn't spot any difference in behavior. Can someone please explain these flags in more detail? Preferably with an example that shows different behavior when flipping one flag to another.
OP preferred an example. Also, what @minaev wrote, was only a part of the story! So, here we go...
Example 1: No (break or last) flags
Result:
Explanation:
For
rewrite
, the flags are optional!Example 2: Outside location block (break or last)
Result:
Explanation:
Outside the location block, both
break
andlast
behave in the exact manner...location
match)Example 3: Inside location block - "break"
Result:
Explanation:
Inside a location block,
break
flag would do the following...location
blockExample 4: Inside location block - "last"
Result:
Explanation:
Inside a location block,
last
flag would do the following...rewrite
result.Summary:
rewrite
condition with the flagbreak
orlast
matches, Nginx stops parsing any morerewrites
!break
orlast
, Nginx does the same job (stops processing anymore rewrite conditions).break
, Nginx only stops processing anymore rewrite conditionslast
, Nginx stops processing anymore rewrite conditions and then starts to look for a new matching oflocation
block! Nginx also ignores anyrewrites
in the newlocation
block!Final Note:
I missed to include some more edge cases (actually common problem with rewrites, such as
500 internal error
). But, that'd be out of scope of this question. Probably, example 1 is out of scope, too!You may have different sets of rewrite rules for different locations. When rewrite module meets
last
, it stops processing the current set and the rewritten request is passed once again to find the appropriate location (and the new set of rewriting rules). If the rule ends withbreak
, the rewriting also stops, but the rewritten request is not passed to another location.That is, if there are two locations: loc1 and loc2, and there's a rewriting rule in loc1 that changes loc1 to loc2 AND ends with
last
, the request will be rewritten and passed to location loc2. If the rule ends withbreak
, it will belong to location loc1.