zoukankan      html  css  js  c++  java
  • 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的错误

  • 相关阅读:
    Neo4j的查询语法笔记(二)
    NEO4J -模糊查询
    mybatis常用jdbcType数据类型
    Codrops 优秀教程:CSS 3D Transforms 实现书本效果
    案例分享:20佳应用大图片背景的优秀网站作品
    分享一套精美的现代 UI PSD 工具包【免费下载】
    经验分享:10个简单实用的 jQuery 代码片段
    Popline:帅气的浮动 HTML5 文本编辑器工具栏
    《分享》学习单页网站制作的20个优秀案例
    Minimit Anima – 硬件加速的 CSS3 动画插件
  • 原文地址:https://www.cnblogs.com/dee0912/p/4687953.html
Copyright © 2011-2022 走看看