Understand NGINX's rewriting break and last, and location matching rules

        location / {
            index  index.html index.htm index.php l.php;
if (!-e $request_filename) {
rewrite /[ac]\d+\.html /index/index/home last;
rewrite ^/admin$ /admin/login/login last;
rewrite ^(.*)$ /index.php?s=$1 last;
break; }            autoindex  off;



        }

1. Break will stop subsequent rewriting rules after rewriting the statement: rewrite ^(.*)$ /index.php?s=$1 break; Including other locations, no matching will be performed.

2. Write alone : ​​rewrite ^(.*)$ /index.php?s=$1 last;
break;

  will only stop the rewrite rules for this location. Other locations will be executed with the rewritten url

3.last will re-match all rewrite rules with the rewritten url. Including in this location means rematching in the entire server.

4. Note: It is very important to note that the rewrite rule will first match the outer layer of location, such as location =/1.php{rewrite ^(.*)$ /2.php last; , location ~ \.php(.* )$ , location /flag { rewrite ^(.*)$ /1.php last; . Three rules. Accessing http://localhost/flag/1.php will not access 2.php

Instead, it appears: No input file specified. Because the location ~ \.php(.*)$ rule has been matched. The file cannot be found because there is no /flag/1.php.

So if you want to access /flag/1.php to get the content of 2.php: modify location /flag { rewrite ^(.*)$ /1.php last to  location ^~  /flag { rewrite ^(.*)$ / 1. php last

5. The url matching priority of nginx location:

            1, = First, the exact match has the highest priority

            2, ^~ followed by a match starting with a specific regular string, this is not a regular

            3. ~ , ~*, !~, !~* are regular matching in order, followed by case-sensitive regular matching, case-insensitive regular matching, case-sensitive regular matching, indistinguishable Regular mismatch of upper and lower case,

            4. The last is the universal character matching of /

            The above is the url matching priority of the nginx location

Location priority example

The configuration items are as follows:

 
 
  1. location = / {
  2. # 仅仅匹配请求 /
  3. [ configuration A ]
  4. }
  5. location / {
  6. # 匹配所有以 / 开头的请求。
  7. # 但是如果有更长的同类型的表达式,则选择更长的表达式。
  8. # 如果有正则表达式可以匹配,则优先匹配正则表达式。
  9. [ configuration B ]
  10. }
  11. location /documents/ {
  12. # 匹配所有以 /documents/ 开头的请求。
  13. # 但是如果有更长的同类型的表达式,则选择更长的表达式。
  14. # 如果有正则表达式可以匹配,则优先匹配正则表达式。
  15. [ configuration C ]
  16. }
  17. location ^~ /images/ {
  18. # 匹配所有以 /images/ 开头的表达式,如果匹配成功,则停止匹配查找。
  19. # 所以,即便有符合的正则表达式location,也不会被使用
  20. [ configuration D ]
  21. }
  22. location ~* \.(gif|jpg|jpeg){
  23. # 匹配所有以 gif jpg jpeg结尾的请求。
  24. # 但是 以 /images/开头的请求,将使用 Configuration D
  25. [ configuration E ]
  26. }

Request matching example

 
 
  1. / -> configuration A
  2. /index.html -> configuration B
  3. /documents/document.html -> configuration C
  4. /images/1.gif -> configuration D
  5. /documents/1.jpg -> configuration E

Note that the above matches have nothing to do with the order defined in the configuration file.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325652116&siteId=291194637