Take the 2-minute tour ×

The nginx wiki states:


Is this to say that:

If the rewrite pattern matches, process the rewrite (rewrite to target) but do not process any of the other rules in the location block, and process all other (cache, proxy, etc) directives in the location block?

I am talking about the break flag as per:


Not the "break;" directive.


The scenario I was thinking of using this for is as follows, a simple location with multiple rewrites:

location /path/ {
    rewrite ^/user_request_one$ /target_one;
    rewrite ^/user_request_two$ /target_two;
    ... other directives ...

Perhaps I dont understand fully how nginx handles rewrites, but in the above, if target_one is matched, will nginx still test rewrite rule target_two? Would adding a break flag to target_one instruct nginx to stop parsing any further rewrite rules?

The main reason I am asking is because we have some location blocks with a number of rewrite rules and I am unsure weather nginx is testing rewrite patterns "further down" even if one is matched "near the top". These rewrites are very unique, so if one is hit, there is absolutely no reason for nginx to test any others in the same location block. We just want to make sure we are not wasting any precious cpu cycles as this is a fairly loaded server.

Hope this helps.


share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There is a good explanation on the HttpRewriteModule page: http://wiki.nginx.org/HttpRewriteModule#rewrite

It's possibly a good idea to have a look at "How nginx processes a request" first: http://nginx.org/en/docs/http/request_processing.html

EDIT: yes, your interpretation is what break does

break will keep the request processing within the same location directive so you can enforce how it is handled even if the rewritten path would, under normal circumstances (i.e. being requested directly), be handled by a different location directive. Note that this only happens when used inside a location directive.

Sorry I couldn't expand further, I'm running out the door but feel free to ask for details of any part of my answer.

share|improve this answer
The reason I am asking is as follows: Imagine a simple location block with 2 100% unique rewrites. Would it be wise to "rewrite ... break;" each of them so that if the first one matches, no cpu cycles are wasted checking the 2nd one? –  anonymous-one Sep 11 '12 at 8:10
Do you mean that you'd like to use the break flag purely to stop checking of the other location directive? If so, it's too late in the handling for this (see the second link). If not, please post the relevant config and, where appropriate, comments re what you're trying to achieve. –  Arran Schlosberg Sep 12 '12 at 8:54
Done :) Thank you kindly for the help thus far btw. –  anonymous-one Sep 12 '12 at 10:51
In the case that you've added in your update the break flag will be appropriate for stopping further checks within the same location. Without knowing the exact paths it's hard to judge the best approach but it may even be better to use regular expressions in your location paths. –  Arran Schlosberg Sep 12 '12 at 11:04
Once again, thank you. –  anonymous-one Sep 12 '12 at 11:06
add comment

Your Answer


