Analysis of the difference between last and break during rewrite writing of nginx configuration _nginx

Source: Internet
Author: User
Tags echo command

In the use of Nginx configuration rewrite often encountered in places where the last and can not work, replaced by a break, which is the principle of the root directory of the understanding of a difference, according to my test results are roughly the same.

Location/  
{  
  proxy_pass http://test;  
  alias/home/html/;  
  root/home/html;  
  Rewrite "^/a/(. *) \.html$"/1.html last;  
} 

In location/{configuration:
1, use root to specify the source: Use last and break can be
2, use Proxy_pass to specify the source: Use last and break can be
3. Use alias to specify source: must use Last
In location/a/or using regular location ~ ^/a/:
1, use root to specify the source: Use last and break can be
2, use Proxy_pass to specify the source: Use break and last results are different
3. Use alias to specify source: must use Last
The difference is mainly on the Proxy_pass label, and then look at a few test results:

Location/  
{  
  root/home/html;  
}  
 
location/a/  
{  
  proxy_pass http://test;  
  Rewrite "^/a/(. *) \.html$"/1.html last;  
} 

In this configuration, using the last access is accessible, but the result is:/home/html/1.html; But what I need is http://test/1.html? Use the break to do it.

Location/  
{  
  root/home/html;  
}  
  
location/a/  
{  
  proxy_pass http://test;  
  Rewrite "^/a/(. *) \.html$"/a/1.html last;  
} 

In this configuration, an error is returned, because last will initiate the request match, so a dead loop is created, and the http://test/a/1.html can be accessed using the break.
So, by using last, the server tag is restarted, and the break is accessed directly using the data source in the current location, depending on the situation. Generally in the location configuration rewrite, are used by the break, and the root of the location use last is better, because if the fastcgi or proxy access to the JSP file, the root location under the break is inaccessible. Test to rewrite when there is a problem, you might as well change the two for a try.
As for the use of alias why must use last, it is estimated that the nginx itself is limited, how to try the break can not succeed.

So let's understand the difference between last and break:
Last: Stop the current request and initiate a request based on the rewrite matching rule. The new request starts again from the first stage ...
Break: Relative Last,break does not relaunch a request, simply skips the current rewrite phase and executes the subsequent execution phase of this request ...

Let's look at one more example:

server {
  listen 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, the break is to skip the rewrite phase of the current request and continue with the other stages of this request, and it is clear that the output for the content phase of the/foo corresponds to the echo "Break page"; (content phase, can be simply understood as the production of data output phase, such as the return of static page content is also in the contents phase; The Echo directive is also run in the content phase, where the content phase can only correspond to one output instruction, Like a location configuration of two Echo, the end will only have one echo command to be executed); Of course, if you annotate the echo command in/break/and then visit/break/xx again, it's the same as we expected: Although/BREAK/XX is redirected to /test/xx, but the break instruction does not reopen a new request to continue the match, so Nginx is not matched to the following/test/location; In the case of the echo instruction being annotated,/break/ This location can only execute nginx default content instructions, that is, try to find/test/xx this HTML page and output content, in fact, this page does not exist, so will report 404 of the error.

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.