Detailed location of the nginx

location positioning means, according to different positioning uri, in the virtual host is essential, location may be different parts of the site location, positioned on a different approach.

location classification match

Precision match

Precisely matched to the number as a symbol =

location = /index.htm  {
    root /var/www/html/;
    index index.htm index.html;
}

location = /index.htm  {
    root html/;
    index index.htm index.html;
}
精准匹配的优先级要优于一般匹配,所以重启nginx后会打开/var/www/html下面的index.htm而不会打开html下的index.htm

General match

location / {
    root /usr/local/nginx/html;
    index index.htm index.html;
}

location /apis {
    root /var/www/html;
    index index.html;
}
我们访问http://localhost/apis/
对于uri的/apis,两个location的pattern都可以匹配它们
即‘/’能够左前缀匹配,"/apis"也能够左前缀匹配
但此时最终访问的是目录/var/www/html下的文件
因为apis/匹配的更长,因此使用该目录下的文件

Regular match

Regular ~ symbol sign matches

location / {
    root /usr/local/nginx/html;
    index index.html index.htm;
}

location ~ image {
    root /var/www/;
    index index.html;
}
如果我们访问,http://localhost/image/logo.png
此时"/"与 location /匹配成功
此时"image"正则与"image/logo.png"也匹配成功?谁发挥作用呢?
正则表达式的成果将会使用,会覆盖前面的匹配
图片会真正的返回/var/www/image/logo.png

to sum up

  • 1. First, determine the precise hit, if hit immediately return a result and end the resolution process
  • 2. Common hit judgment, if there are multiple hits, hits the longest recorded, (but not the end of the recording, the longest accurately)
  • 3. Continue determination result of analysis of the regular expression, expression in the order arranged by positive subject, from top to bottom to match begins, once a successful match, return the results immediately, and ends the parsing process.
  • 4. Common hit the order does not matter, determined according to the length of the hit
  • The so-called regular hit, hit matching front to back

Guess you like

Origin www.cnblogs.com/lisqiong/p/11414082.html