nginx rewrite instruction last break most detailed explanation of the difference between

Summary: server location may rewrite block, with regular than when it is overwritten, so that a plurality of sequential rewrite matching front and preferentially executed.

break out of the rewrite stage, not in the match, into the output stage. last re-initiate similar request, it will re-match.

 

 

 

nginx's official comment is this:

last
   stops processing the current set of ngx_http_rewrite_module directives followed by a search for a new location matching the changed URI;

break
   stops processing the current set of ngx_http_rewrite_module directives;

 

We know that nginx is running in ten phase of implementation, ngx_http_rewrite_mode said above-mentioned, can be understood as one stage -rewrite stage.

typedef enum {
    NGX_HTTP_POST_READ_PHASE = 0,
    NGX_HTTP_SERVER_REWRITE_PHASE,
    NGX_HTTP_FIND_CONFIG_PHASE,
    NGX_HTTP_REWRITE_PHASE,           //rewrite阶段在这里
    NGX_HTTP_POST_REWRITE_PHASE,
    NGX_HTTP_PREACCESS_PHASE,
    NGX_HTTP_ACCESS_PHASE,
    NGX_HTTP_POST_ACCESS_PHASE,
    NGX_HTTP_TRY_FILES_PHASE,
    NGX_HTTP_CONTENT_PHASE,
    NGX_HTTP_LOG_PHASE
} ngx_http_phases;

 

So let's understand the difference and break the last:
last: stop the current request, and re-initiate a request to rewrite the rules of the match. The new request and from the first phase started ...
BREAK: relative last, break and will not re-initiate a request, just skip the current rewrite stage, and the subsequent implementation phase of this request ...

 

Let's look at an example:

server {
    listen 80 default_server;
    server_name dcshi.com;
    root www;

    location /break/ {
        rewrite ^/break/(.*) /test/$1 break;
        echo "break page";
    } 

    location /last/ {
         rewrite ^/last/(.*) /test/$1 last;
         echo "last page";
    }    

    location /test/ {
       echo "test page";
    }
}

 

Request: http://dcshi.com/break/***
Output:  BREAK Page
Analysis: As discussed mentioned above, break skip rewrite phase current request, and to continue with other requests this phase, obviously, for the I / O content phase foo corresponding to echo "break page"; (content stage, can be simply understood as a phase generating output data as return static page content is in the content stage; echo command is run in the content stages, under normal circumstances content output stage corresponds to only one instruction, two echo configuration as a location, it will eventually have an echo instruction is executed); of course, if you put / break / echo command in the comments, and then visit / break / xx will be reported again 404, which is the same as with our expectations: Although / break / xx is redirected to / test / xx, but the break command does not re-open a new request continues to match, so nginx is not matched to the following / test / this location; in the case of echo command annotated, / break / nginx can only be performed in this location the default content instruction, that is, try to find / test / xx this html page and output from within Yung, in fact, this page does not exist, it will be reported 404 errors.

 

Request:  http://dcshi.com/last/***
output:  the Test Page
Analysis: The  biggest difference between last and break Shi, last will re-initiate a new request, and re-matching location, so for / last, re-match request after the matches / test /, the output of the final stage corresponding to the content is test page;

 

Suppose you have a general understanding of the operational phase of nginx, to understand the last break with no problem.

 

 

location ~ ^/testtest/ {

default_type text/html;

echo 111;

}

rewrite ^/testtest/  /test.php last;

 

Access / testtest / when the value of the output is /test.php content, apparently, to rewrite the rewrite, and rewrite the exchange location and position, the results of the same, irrespective of the description and location.

Published 75 original articles · won praise 41 · views 390 000 +

Guess you like

Origin blog.csdn.net/Erica_1230/article/details/103965032