004_Nginx rewrtie four flag

Nginx rewrtie four flag

Using the nginx rewrite instruction, the steering can be achieved url, for the four different rewrtie In Flag, respectively, redirect, permanent, break and last. Of which the first two are jumping type of flag, the latter two is the agent type. Jump type refers to have the client browser to re-request a new address, type of proxy is to achieve a jump in internal WEB server.

redirect: 302 to jump to the back rewrtie address.

permanent: 301 permanent adjustment to the back rewrtie address, current address has been permanently moved to a new address, typically to search engine friendly.

last: The address of the server to perform again rewrite the label.

break: The address re-executed in the current location label after rewrite.

Proxy_pass root or use the specified source, last, break can be, but the results may differ, with the later examples; alias is used to specify the source, must be last. If the following configuration:

location / {
    root   /var/www/html;
    index  index.html index.htm;
    rewrite "/x/t.html" /y/t.html break;
}
location /y/ {
    root  /var/www/html/other;
}

When requested /x/t.html, rewrite rule compliance; address so that adjustments are made to /y/t.html, since the flag using a BREAK, so jump in "location /", the result of the / var /www/html/y/t.html. However, if the flag for the last, then /y/t.html will re-execute the server tab, a "location / y /", so the jump was re-assigned to "location / y /" label because of, so this time results /var/www/html/other/y/t.html.

Note: Use the last, if configured incorrectly, you may cause an infinite loop. E.g:

location /x/ {
    proxy_pass http://my_proxy;
    rewrite "^/x/(.*)\.html$" /x/1.html last;
}

Reprint please indicate the source of the article, thank you! Reprinted from: http://www.netingcn.com/nginx-rewrite-flag.html


Nginx 中 last、break、permanent、redirect

1. last 和 break

  • location outside

When the last and when the break occurs outside the location, both the effect is the same no difference. Attention to the fact that they will skip all after their rewrite module command, to choose their own matching location.

Example:

rewrite url1 url2 last; ①
rewrite url3 url4 last; ②
rewrite url5 url6 last; ③

location ~  url2   ④
location ~  url4   ⑤
location ~  url6   ⑥

When this rewrite rule takes effect ①, ② and ③ behind it will not be skipped judgment, away directly behind the choice of location; There may be a doubt that those instruction input rewrite module command it? If using nginx itself, you have to go to the official website of the query. But if you are using a tengine, you can use tengine -V. Information you will want listed.

  • Location within

When the last break and when there is inside the location, there is a difference between the two.
last: Use the last command will jump after the rewrite location scope, just that once again resumed the behavior of
break: Use the break command, will not jump out of the scope of location after rewrite. It is also the location of the end of this life.

Example:

rewrite xxx1 yyy last; ⑦
rewrite xxx2 yyy last; ⑧
rewrite xxx3 yyy last; ⑨
rewrite xxx4 yyy last; ⑩

location ~  url1
{
    rewrite url1 url2 last; ①
}

location ~  url2
{
    rewrite url3 url4 break; ②
    fastcgi_pass 127.0.0.1:9000;
}

These examples:
after the first location of the rewrite instruction processing is completed, will pop up location, and then re-determined rewrite rules 7 to 9;
after the second rewrite instruction in location processing is completed, will not jump location, not to re- Analyzing the rule rewrite 7 ~ 9. But only to pass information to the back of fastcgi_pass instruction or the like proxy_pass

2. permanent and redirect

permanent: permanent redirect. Log request status code 301

redirect: Temporary Redirect. Log request status code 302

From the point of view to see the realization of functions, permanent and redirect the same. Where there is no good, where bad; nothing performance problems exist. (Or when a Baidu crawl your site) but SEO. Something like this, you will in the end be permanent or temporary redirection redirection of interest. Can not understand, require in-depth, google it.

3. last、break VS permanent、redirect

Refers to the status code 301 and 302 and the permanent redirect; the last and then break with respect to the request status code 200 for access log

These two types of keywords, what difference we can see is the eye it? Let me give you an example of it:

  • When you open a Web page, and open the debug mode, you will find behaviors 301 and 302 of the case. After the first request 301 or 302, the browser re-acquired a new URL, then the new URL will re-visit. So when you are configuring a permanent and redirect, you visit a URL request to land on the server is at least two times.

  • When you configure the last or break, after you finalized the final URL, this URL will not be returned to the browser, but it threw fastcgi_pass or proxy_pass instructions to deal with. Request a URL, the number fell to 1 on the server times.

Reprint please indicate the source of the article, thank you! Transfer: http://blog.51cto.com/unixman/1711943

Guess you like

Origin www.cnblogs.com/cy-8593/p/12333623.html