• nginx的location规则(一)


    nginx的location规则(一)

         nginx的url配置是使用nginx最基本功能。nginx作为服务器,它可以接收请求, 处理请求,都是基于客户端url访问。掌握url的配置要了解配置的几个指令。熟悉每个匹配模式的特点。

         之前对于nginx的location匹配规则,我是一塌糊涂,最近认真学了一下,稍微清晰一点了,先记录下来,方便以后再来查看。

    1、location的介绍

    location用来根据URI来进行不同的定位。通过它可以把网站的不同部分,定位到不同的处理方式上,比如碰到.php,调用php解释器。

    2、location的语法

    location [=|~|~*|^~] patt {}    中括号可以不写任何参数,此时称为一般匹配。

    符号说明:
    1) =         表示精确匹配

    2) ^~      表示uri以指定字符或字符串开头的前缀匹配,不是正则匹配。一旦匹配成功,则不再查找其他匹配项

    3) ~         表示区分大小写的正则匹配

    4) ~*       表示不区分大小写的正则匹配

    5) /         表示通用匹配, 如果没有其它匹配,任何请求都会匹配到

    规则优先级:
    = 高于 ^~ 高于 ~* 等于 ~ 高于 /

    因此,大类型可以分为3种

    1)location = patt{}  [精准匹配]

    2)location   patt{}   [一般匹配]

    3)location ~ patt{}  [正则匹配]

    3、匹配规则

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

    location = patt{

       config A

    }

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

    4、举例

    在下面举例开始之前,先说明一个事实:访问域名test.php7.isee.wangtest.php7.isee.wang/效果是一样。

    原因:1)访问域名,其实是会由DNS解析服务,解析成对应服务器的ip地址。在这个ip地址,我们所要访问的是服务器目录下的文件。

              2)因此,直接访问域名或者域名后面跟”/“,都是在根目录/处进行访问,所以效果是一样的。

    4.1  配置举例1

    1       location = / {
    2 
    3         root /www/wwwroot/test.php7.isee.wang/html/;   #目录
    4 
    5         index index.htm  index.html;    #索引引导页
    6 
    7       }

    访问域名test.php7.isee.wang

    报404,那么我们就来查看下错误日志

    执行命令:tail -f /www/wwwlogs/test.php7.isee.wang.error.log

    分析:location 后面的 ”= /“ 表示精确匹配/,但是斜线只是根目录,后面没有紧跟要访问的实际目录或者文件,所以这里的root没发挥作用,最终引导到索引页index.htm=>test.php7.isee.wang/index.htm

    最终结果,访问了/www/server/nginx/html/index.htm(nginx全局服务器默认目录) 

    定位流程:

    1、精准匹配中 “/”,定位到引导页index.htm

    2、再次访问/index.htm,此次内部跳转已经是“/index.htm”,根目录为/www/server/nginx/html,但是我这里该目录下没有index.htm,所以报404

     

    4.2 配置举例2

    1       location = /index.htm  {
    2 
    3         root /www/wwwroot/test.php7.isee.wang/html/;
    4 
    5         index index.htm  index.html;
    6 
    7       }

    访问test.php7.isee.wang/index.htm

     分析:1)这里精确匹配index.htm,这个时候root就发挥作用了,此时会定位到/www/wwwroot/test.php7.isee.wang/html/index.htm

               2)为了方便直观查看,我在做此配置之前就现在对应的index.htm中的内容就是写的所在目录。

               3)如果匹配不到相应的location,则会继承整体nginx服务器的配置,即定位到/www/server/nginx/html/index.html

     

    4.3 配置举例3

    location中如果不配置root,就会继承nginx全局服务器的配置/www/server/nginx/html/index.html

     1 #定位1      
     2 location = /index.htm  {
     3       root /www/wwwroot/test.php7.isee.wang/html/; 
     4       index index.htm  index.html;
     5 }
     6 
     7 #定位2
     8 location = / {
     9       root   /var/www/html/;
    10       index  index.htm  index.html;
    11 }
    12 #定位3
    13 location /index.htm {
    14       root   html;
    15       index  index.html index.htm;
    16 }

    访问网址:

    定位流程见下图:首先是精确匹配到规则2,然后访问索引引导页index.htm,然后精确匹配到规则1

    4.4  配置举例4

     1 #定位1
     2 location = /index.htm  {
     3     root /www/wwwroot/test.php7.isee.wang/html/;
     4     index index.htm  index.html;
     5 }
     6 #定位2
     7 location = / {
     8     root   /var/www/html;
     9     index  index.html  index.htm;
    10 }
    11 #定位3
    12  location /index.htm {
    13     root   html;
    14     index  index.html index.htm;
    15 }

    访问test.php7.isee.wang

    匹配规则:

     定位流程:访问域名->匹配规则2->索引引导页index.html->匹配规则3

     注意:

     1、规则3是普通匹配,index.html包含了index.htm这9个字母,所以能匹配上

     2、规则3的root对应的html目录,其全名应该是nginx全局默认目录:/www/server/nginx/html/

    4.5 配置举例5

     1  location = /index.html  {
     2     root /www/wwwroot/test.php7.isee.wang/html/;
     3     index index.htm  index.html;
     4  }
     5 
     6  location = / {
     7     root   /var/www/html;
     8     index  index.html  index.htm;
     9  }
    10 
    11  location /index.htm {
    12     root   html;
    13     index  index.html index.htm;
    14  }

    访问 test.php7.isee.wang

     匹配规则:

     

     定位流程:访问域名->匹配规则2->索引引导页index.html->匹配规则1(因为精确匹配的优先级是最高,因此这里优先匹配规则1,所以访问的是规则1中root目录下的index.html)

  • 相关阅读:
    桥梁模式
    原型模式
    css backgroundposition
    eclipse配置了maven,项目报错
    SQL 练习题目
    Springmvc + Ibatis 搭建 点餐系统
    Delphi 性能优化工具
    Delphi的接口陷阱
    delphi 内存管理,定期释放
    Delphi制作数据感知控件之浮想联翩
  • 原文地址:https://www.cnblogs.com/hld123/p/13926258.html
Copyright © 2020-2023  润新知