03_nginx之location

location

location 有”定位”的意思,,根据Uri来进行不同的定位,在虚拟主机的配置中,是必不可少的。
location可以把网站的不同部分,定位到不同的处理方式上。比如, 碰到.php, 如何调用PHP解释器? --这时就需要location

  • location 的语法
location [=|~|~*|^~] patt {
}

中括号可以不写任何参数,此时称为一般匹配

也可以写参数,因此,大类型可以分为3种:

location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]

精准匹配

  • 如何发挥作用?:
    首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
    config A
}

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

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

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

上面都是相对路径,绝对路径为usr/local/nginx/www/usr/local/nginx/html
如果访问:http://z.com/

  • 定位流程是

    1. 精准匹配中 =/, 根目录为usr/local/nginx/www ,得到index页为index.html
    2. 再次访问 /index.html , 此次内部转跳uri已经是/index.html
    3. 此时一般匹配 / 生效,根目录为/usr/local/nginx/html
    4. 最终结果,访问了 /usr/local/nginx/html/index.html
  • 精准匹配

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

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

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

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

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

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

正则表达式

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

        location ~ image {
            root    www;
            index   index.html index.htm;
        }

如果我们访问 http://z.com/image/logo.png
此时, //image/logo.png 匹配,同时,image正则 与image/logo.png也能匹配,谁发挥作用?
正则表达式的成果将会使用,图片真正会访问 /usr/local/www/image/logo.png

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

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

我们访问 http://z.com/foo
对于uri /foo, 两个location的patt,都能匹配他们,即 /能从左前缀匹配 /foo, /foo也能左前缀匹配/foo,
此时, 真正访问 /usr/local/www/index.html,原因:/foo匹配的更长,因此使用之;

总结

location解析过程
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,967评论 19 139
  • 配置运行Nginx服务器用户(组) 用于配置运行Nginx服务器用户(组)的指令是user,其语法格式为: use...
    吃瓜的东阅读 4,537评论 0 41
  • 上一篇《WEB请求处理一:浏览器请求发起处理》,我们讲述了浏览器端请求发起过程,通过DNS域名解析服务器IP,并建...
    七寸知架构阅读 81,245评论 21 356
  • 1.ngnix介绍 ngnix www服务软件 俄罗斯人开发 开源 性能很高 本身是一款静态WWW软件 静态小文件...
    逗比punk阅读 2,131评论 1 6
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,896评论 0 3