nginx(三十四)root和alias指令辨析

一    root指令

①   案例一  简单

$uri:'/resource/href.html'

location:'/resource'

  -- 强调:location'只能'匹配uri,'不能'匹配'参数'

最终'资源'文件:/usr/share/nginx/html/resource/href.html

最终os的'资源路径':是'root'指定的值'+'匹配对应location的'$uri'值

++++++++++ 只有使用$request_uri 才能获取'完整'的链接 ++++++++++ 

if ($request_uri ~* ^/Handler/FileHandler.ashx\?([^_]+)(_[0-9]+)$) {

    set $m1 $1;

    rewrite ^/Handler/FileHandler.ashx /Handler/FileHandler.ashx?$m1? permanent;
}

增强认知:'$request_uri'为什么不记录'锚点[#]'信息,因为'浏览器根本没'把'锚点'发送给服务器

 ②   案例二  复杂

二  alias

1. alias会把location后面'配置的路径'丢弃掉

2. 把当前匹配到的目录'指向'到指定的目录

3. 而 alias 正如其名,alias指定的路径是'location的别名',不管location的值怎么写,资源的'真实路径'都是 alias 指定的路径

nginx配置alias时加不加/

用alias时里面加/,外面就要加/;里面不加外面也不能加;alias代理的时整个完整路径,包括最后的/.

location /user {  
     alias    /image/user;  
     autoindex on;  
}    
#或者#

location /user/ {  
      alias   /image/user/;  
      autoindex on;  
}

①  普通

1)最简单

$uri: /v1/mnt.html

location:/v1/

alias: /usr/share/nginx/html/

最终路径:/usr/share/nginx/html/mnt.html

解读:'location中'映射的就是'alias'值;解决了'uri'中的部分路径不是'磁盘路径',通过'alias'进行删除

 2)稍微复杂

$uri: '/v1/["location中"]'test/mnt.html

location:/v1/

alias: /usr/share/nginx/html/

最终路径:'/usr/share/nginx/html/["实际"]'test/mnt.html

解读:'location中'映射的就是'alias'数值

深刻理解:'alias'和'location'是一一'映射'的,alias是location的'别名'

②  重复

++++++++++++++  "等价"  ++++++++++++++

location /v2/ {
  root /usr/share/nginx/html;
}

③   正则测试

三    alias和root的小结

[1].  root与alias'主要区别'在于nginx如何'解释'location后面的uri

[2].  这会使两者分别以'不同的方式'将请求'映射'到'服务器资源文件'上

在location中使用root和alias

 四    $document_root

1、'当前请求'下["一般uri是对应location下"]

2、由于'alias'作用域是location,如果location定义了'alias',则是alias值

备注:如果'alias'和'root'同一作用域同时存在,'谁再后'谁生效

3、如果'location'都没有定义'root'和'alias',则继承'root',是'父'root的值

4、如果'location定义root',则是'覆盖父root',

①  测试

说明:揉和到一个'配置中'测试

②  测试1

③  测试2

④  测试3

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值