nginx location配置规则学习

最近学习vuejs,作先后端分离,demo作完了,最终仍是要发布的,因而学习nginx,其它配置却是还好理解,惟独对localtion理解不了,最后请教了一些网友,才得以解决问题,可是不但愿下次还遇到问题,因此这里把localtion的配置规则,都尝试一遍,也记录一下,方便本身,也方便别人。html

一、配置规则

只要在网上搜一下,或者去官网翻一下文档,都能知道有几种规则,这是官网的:vue

Syntax:    location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:    —
Context:    server, location

总而言之,包含两部份内容,一部分是匹配前提条件或者叫要求,另一部分是要匹配的东西,也就是匹配内容。nginx

下面这段是官网说明的谷歌翻译:正则表达式

在对“%XX”形式编码的文本进行解码以后,针对归一化的URI执行匹配,解析对相对路径组件“.”和“..”的引用,以及将两个或多个相邻斜杠可能压缩为单个斜杠。

localtion能够由前缀字符串或正则表达式定义。正则表达式使用前面的“〜*”修饰符(不区分大小写匹配)或“〜”修饰符(用于区分大小写匹配)指定。要找到匹配给定请求的位置,nginx首先检查使用前缀字符串(前缀位置)定义的位置。其中,选择并记住具备最长匹配前缀的位置。而后检查正则表达式,按照它们在配置文件中的显示顺序。正则表达式的搜索在第一个匹配中终止,而且使用相应的配置。若是没有找到与正则表达式匹配,则使用以前记住的前缀位置的配置。

localtion blocks能够嵌套,下面提到一些例外。

对于不区分大小写的操做系统,如macOS和Cygwin,与前缀字符串的匹配忽略了一个状况(0.7.7)。可是,比较仅限于一个字节的区域设置。

正则表达式能够包含稍后能够在其余指令中使用的捕获(0.7.40)。

若是最长匹配的前缀位置具备“^〜”修饰符,则不会检查正则表达式。

另外,使用“=”修饰符能够定义一个彻底匹配的URI和位置。若是找到彻底匹配,则搜索终止。例如,若是频繁出现“/”请求,则定义“location = /”将加快对这些请求的处理,由于搜索在第一次比较以后当即终止。这样的位置不能明显地包含嵌套的位置。

在从0.7.1到0.8.41的版本中,若是请求匹配前缀位置而没有“=”和“^〜”修饰符,则搜索也将被终止,而正则表达式未被检查

根据上面的说明可知,默认状况, nginx先检查前缀字符串,而后检查正则表达式,若是前缀字符串匹配到了,而且前缀字符串有这个“^~” 要求,就不配正则了;若是没有这个“^~” ,即便前缀匹配到了,也要去匹配正则表则,若是正则表达式匹配到了,就是用正则表达式的,没有就是用前缀字符串匹配到的路径;后端

二、无正则表达式匹配

规则:使用匹配到的最长的前缀的路径。前后端分离

location  / {
            root   html;
            index  index.html index.htm;
        }
        
        location  /img {
            root   D:/nginx/img;
            index  test.png;
        }

这是nginx下载安装完成后,增长了一个叫img的配置,访问路径是http://localhost/img,访问一下看看什么效果,我开启了日志,就不截图了,直接看日志里,nginx查找的路径:学习

D:/nginx/img/img/test.png" is not found (3: The system cannot find the path specified)

文件查找的路径是D:/nginx/img(这是我配置的路径),下面的img文件下的test.png文件测试

若是改为这样:ui

location  / {
            root   html;
            index  index.html index.htm;
        }
        
        location  /img/ {
            root   D:/nginx/img;
            index  test.png;
        }

看看有没有区别,从新启动,在访问http://localhost/img试一下:编码

"D:/nginx/img/img/test.png" is not found (3: The system cannot find the path specified)

和以前没什么区别,也就是两个效果是同样的,在浏览其中http://localhost/imghttp://localhost/img/是同样的。

再改一下,重启:

location  / {
                root   html;
                index  index.html index.htm;
            }
            
            location  /img/ {
                root   D:/nginx/img/;
                index  test.png;
            }

我在从新访问一下:

"D:/nginx/img/img/test.png" is not found (3: The system cannot find the path specified)

仍是同样的效果。
因此若是是前缀字符串匹配,就不要纠结后面要不要加“/”的问题。
另外从结果上看,确实符合规则,使用前缀最长的匹配路径。

二、有正则匹配,无符号修饰

规则:无“^~”, 最终使用正则匹配到的路径,正则匹配不到,使用字符串前缀匹配最长的

增长一个配置:

location  \/img\/ {
                root   D:/nginx/reg/;
                index  test.png;
            }

重启访问:

"D:/nginx/img/img/test.png" is not found (3: The system cannot find the path specified)

结果发现,找的仍是D:/nginx/img的img文件夹的文件,好像正则没有匹配上,改一下:

location  \/img {
                root   D:/nginx/reg/;
                index  test.png;
            }

这下必定能够匹配上了,重启http://localhost/img测试:

"D:/nginx/img/img/test.png" is not found (3: The system cannot find the path specified)

无语了,为何没有按照规则来呢,为何没有查找D:/nginx/reg/路径下的文件呢?

回到前面看一下官网说明,发现正则表达式须要"~"或者"~*"指定才行,修改成下面的配置:

location ~* \/img\/ {
                root   D:/nginx/reg/;
                index  test.png;
            }

重启访问http://localhost/img测试:

"D:/nginx/reg/img/test.png"

这下按照规则了,看东西看来仍是要认真看啊!

改一下正则表达式:

location ~* \/img {
                root   D:/nginx/reg/;
                index  test.png;
            }

再重启测试:

"D:/nginx/reg/img/test.png" is not found (3: The system cannot find the path specified)

也能够正常匹配,也就是说正则匹配的状况下,最后一个/要不要都行,效果是同样的

另外“~*” 不区分大小写和“~”就不用试,应该都知道

三、精确匹配=号

规则:精确匹配某个路径,优先级最高

增长一种配置:

location = /img/test.png {
                root   D:/nginx/denghao/;
                index  test.png;
            }

重启访问路径http://localhost/img/test.png

"D:/nginx/denghao/img/test.png" failed (3: The system cannot find the path specified)

从结果看出,和规则一致;

改一下配置:

location = /img/ {
                    root   D:/nginx/wuhouzhui/;
                    index  test.png;
                }

测试结果:

"D:/nginx/reg/img/test.png" is not found (3: The system cannot find the path specified)

从结果上看,最终使用的匹配路径是正则,至于 = /img/,有没有匹配到,我也不清楚,总而言之,这种状况下使用的是正则的路径,有知道朋友解释一下=号有没有匹配到。

四、提升前缀字符串的优先级的“^~”

规则:若是最长匹配的前缀位置具备“^〜”修饰符,则不会检查正则表达式

由于先搜索匹配的是前缀字符串,全部匹配到了,有这个修饰符就不检查正则了,因此正则就不考虑了。
把配置改一下:

location ^~ /img/ {
                root   D:/nginx/img/;
                index  test.png;
            }
            
            location = /img/test.png {
                root   D:/nginx/denghao/;
                index  test.png;
            }

访问http://localhost/img/test.png测试:

"D:/nginx/denghao/img/test.png" failed (3: The system cannot find the path specified)

能够看到匹配结果是等号的路径,因此=号的优先级比^~高

五、总结

搜索优先级:

精确匹配 > 字符串匹配( 长 > 短 [ 注: ^~ 匹配则中止匹配 ]) > 正则匹配( 上 > 下 )

使用优先级:

精确匹配 > (^~) > 正则匹配( 上 > 下 )>字符串(长 > 端)

刚学的,若是有错误,请指出,我修改,别让错误误导他人。

相关文章
相关标签/搜索