Nginx last and break difference, nginxbreak

Source: Internet
Author: User
Tags echo command

Nginx last and break difference, nginxbreak

Today, at the official website of the Development Company: http://www.zstime.com/, when a problem occurs, when writing a rewrite, it turns out that lastis and break are used,

Later I checked the information and found that the main differences are:


Last: stop the current request and re-initiate a request based on the rewrite matching rules. The new request starts from the first stage...
Break: Compared to last, break does not initiate a new request, but skips the current rewrite phase and executes the subsequent execution 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 above, break skips the rewrite phase of the current request and continues to execute other stages of this request. Obviously, the output of the content phase corresponding to/foo is echo "break page". (The content stage can be simply understood as the stage for generating data output. For example, returning static page content is also in the content stage; the echo command is also running in the content stage. Generally, the content stage can only correspond to one output command. Just like configuring two echo in a location, only one echo command is executed ); of course, if you comment out the echo command in/break/And then access/break/xx again, 404 will be reported, which is also as expected: although/break/xx is redirected to/test/xx, the break command does not enable a new request to continue matching, so nginx does not match the location of/test/below; when the echo command is commented,/break/in this location You can only execute the default nginx content command, that is, try to find the html page/test/xx and output the content. In fact, this page does not exist, so a 404 error will be reported.

 

Request:Http://dcshi.com/last /***
Output:Test page
Analysis:The biggest difference between last and break is that last will initiate a new request and rematch the location. Therefore, for/last, the request will match/test /, therefore, the final output of the corresponding content stage is test page;


What are the differences between redirect permanent in nginx rewrite?

In the nginx rewrite command, you can set the rewrite flag to describe the rule. Generally, you can set four types of flags: last, break, redirect, and permanent.

Redirect indicates that this rule is a temporary jump, and if you observe the http request, the http response status code is 302.
Permanent indicates that this rule is a permanent jump, and the http response status code is 301.

So what is permanent jump and temporary jump? The following is an example:
If there is a url,/.
If you configure
Rewrite "/a" "test.html" redirect; indicates that this jump is a temporary jump. If a web crawler crawls this link, it will not update its own url database.
If permanet is configured, The crawler updates its own url database and updates/a to test.html.
This is the difference between a temporary jump and a permanent jump.

 
It seems that nginx's pseudo-static rules are the same as apache's pseudo-static rules, but they are more than last. Right?

In terms of functions, there is no difference between the two, just as different languages can express the same meaning.
However, in terms of writing form, I personally prefer the nginx rewrite method, because the nginx routing rules are easier to write and easy to understand.

Rewrite regex placement [flag]; for flag, nginx does not only have last, but also has break, redirect, and permanent. These logos have different functions. In addition, I rarely use last when using it, instead of break.
Redirect generates a 302 jump, while permanent generates a 301 jump.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.