Nginx 笔记与总结(6)Location:精准匹配

在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 .php 文件如何调用 PHP 解释器。

location 语法:

location [=|~|~*|^~] /uri/ { … }

location 类型分为:

location = patt {}[精准匹配]

location patt {}[一般匹配]

location = ~patt {}[正则匹配]

精准匹配

匹配时首先看有没有精准匹配,如果有,则停止匹配过程:

location = patt {

  config A

}

如果 $uri == patt ,匹配成功,使用 config A。

【例 step 1】

注释掉 /usr/local/nginx/conf/nginx.conf 中之前配置的 server 信息,在默认的 server 段中进行编辑,此时访问 192.168.254.100,显示

此时 server 段中 location 的配置为:

        location / {
            root   html;
            index  index.html index.htm;
        }

此时,访问 192.168.254.100 实际访问的目录是:/usr/local/nginx/html/index.html

注意:root 中的 html 的绝对路径是 /usr/local/nginx/html,该目录下只有一个 index.html 文件

【例 step 2】

此时在一般匹配 location / 的上文中添加一段精准匹配(优先匹配精准匹配,一旦匹配则立即停止匹配过程),此时的 location 信息为:

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

        location / {
            root   html;
            index  index.html index.htm;
        }

为了以示区别,精准匹配的目录设置为 /var/www,该目录下只有一个文件 index.htm:i‘m /var/www/index.htm

平滑重启 nginx

访问 http://192.168.254.100/,仍然显示

原因是:当输入 192.168.254.100 这个 ip 时,实际上是访问一个文件,通过精准匹配找到该文件是 index.html,也就是说访问 192.168.254.100 即访问 192.168.254.100/index.html,此时只能通过一般匹配访问 html(绝对路径为 /usr/local/nginx/html)下的 index.html ,这就解释了为什么会出现 Welcome to nginx 的页面。

【例 step 3】

但是一旦把精准匹配的 index.html 和 index.htm 调换位置:

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

        location / {
            root   html;
            index  index.html index.htm;
        }

  

访问 http://192.168.254.100/,则会出现 404 Not Found:

查看 /usr/local/nginx/logs/error.log:

提示 /usr/local/nginx/html/index.htm 找不到

【例 step 4】

修改精准匹配的 location,此时的 location 信息为:

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

        location / {
            root   html;
            index  index.html index.htm;
        }

平滑重启 nginx,访问 http://192.168.254.100/index.htm,显示 i‘m /var/www/index.htm

说明发生了精准匹配,匹配到了文件 /var/www/index.htm

【例 step 5】

修改普通匹配的 location,此时的 location 信息为:

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

        location /index.htm {
            root   html;
            index  index.html index.htm;
        }

平滑重启 nginx,访问 http://192.168.254.100/index.htm,显示 i‘m /var/www/index.htm:

说明精确匹配优先于普通匹配。

【例 step 6】

此时直接访问 http://192.168.254.100/,即既不精确匹配,也不普通匹配,访问的实际页面是 /usr/local/nginx/html/index.html:

【例 step 7】

添加一条 location 配置信息(第 2 条),此时的 location 配置信息为:

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

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

        location /index.htm {
            root   html;
            index  index.htm index.html;
        }

此时访问 http://192.168.254.100/,则会命中第 2 条精准匹配,显示:i‘m /var/www/index.htm  

参考:

Linux nginx 配置 location 语法 正则表达式

解决Nginx服务器中403 forbidden的错误

时间: 2024-07-31 14:26:16

Nginx 笔记与总结(6)Location:精准匹配的相关文章

06 nginx Location详解之精准匹配

一:Location详解之精准匹配 location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上. 比如, 碰到.php, 如何调用PHP解释器?  --这时就需要location location 的语法 location [=|~|~*|^~] patt { } 中括号可以不写任何参数,此时称为一般匹配 也可以写参数 因此,大类型可以分为3种 location = p

Nginx得Location配置详解之精准匹配

一.location 的定义 location 有"定位"的意思,根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少得,location可以把网站的不同部分,定位到不同的处理方式上. 二.location 的语法 location [=|~|~*|^~] patt{ } 中括号可以不写任何参数,此时称为一般匹配,也可以写参数 因此,大类新可以分为3种: location=patt{}[精准匹配] location  patt{} [一般匹配] location ~patt{}

Nginx 笔记与总结(7)Location:正则匹配

在 /usr/local/nginx/conf/nginx.conf 的默认 server 段中,保留默认的 location 信息(之前测试的 location 配置删除): location / { root html; index index.html index.htm; } 在 /var/www 下创建 image 目录: [[email protected] ~]# cd /var/www [[email protected] www]# mkdir image 使用 wget 或者

nginx 精准匹配

精准匹配优先级最高 server { listen       80; server_name  localhost; location / { root   html;              #相对路径,也就是nginx的安装路径 index  index.html index.htm; } location = / { root /usr/local/w/; index index.html; } 这个如果在浏览器里访问http://127.0.0.1/的话仍然会匹配到/usr/loca

Nginx 笔记与总结(8)Location:归纳总结

首先对 URI 进行精准匹配,如果匹配上则退出匹配,返回精准匹配结果: 如果没有匹配上则寻找普通匹配,如果命中多个普通匹配,则记忆最长的匹配结果(不考虑 location 的顺序): 如果后面还有正则匹配,则按照正则匹配的 location 先后顺序,先匹配上的发挥作用,返回正则命中结果: 如果正则没有匹配上,则记忆最长的匹配结果(普通匹配)发挥作用

Nginx配置中Location的匹配规则

Location匹配的url的语法规则: location [=|~|~*|^~] /uri/ { … } = 表示精确匹配 ~ 表示区分大小写的正则匹配 ^~ 表示以某个常规字符串开头的url即可: ~* 表示不区分大消息的正则匹配 !~(*!~)表示不区分大小写不匹配的正则 / 通配符,任何请求都能匹配到这个location,如果有任何其他location可以匹配,则会覆盖该location 匹配顺序: 1)先匹配普通url,在匹配正则 2)“普通 location ”的匹配规则是“最大前缀

Nginx 之 location 指令匹配规则

location 指令的匹配命令如下: ~,执行一个区分大小写的正则匹配. ~*,执行一个不区分大小写的正则匹配. ^~,普通字符匹配,通常用于匹配目录. =,普通字符精确匹配. @,定义一个命名的 location,用在内部定向中. 例如 error_page 或 try_files 中. location 上下文的优先级与它在 nginx.conf 文件中的位置无关,只与正则表达式的类型有关.对于相同类型的表达式,会优先匹配字符串长的 location. 第一优先级,= 类型表达式.一旦匹配

Nginx location指令匹配顺序规则

location匹配命令 1. "= ",字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. "^~ ",最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何前缀:最大前缀匹配,举例如下: location /  代表以"/"开头的搜索匹配, 再没有正则表达式匹配的情况下才进行这个匹配(优先级最低) 4. "~ ",大小写相关的正则匹配 5. "~* " , 大小写

前端开发掌握nginx常用功能之server&location匹配规则

nginx主要是公司运维同学必须掌握的知识,涉及到反向代理.负载均衡等服务器配置.前端开发尤其是纯前端开发来说对nginx接触的并不多,但是在一些情况下,nginx还是需要前端自己来搞:例如我们公司的开发环境和测试环境,虽然qa可以帮助搞定配置,但是每新增一个前端模块或者模块nginx配置经常变更都求着qa搞,麻烦别人还不如自己来搞,这样更能理解自己的需求.这些都需要前端开发对nginx有所理解,下面我们来说说nginx最基础的server和location匹配规则. 1. server匹配规则