一 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 指定的路径
复制
用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'值
复制
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;}
复制
③ 正则测试
[1]. root与alias'主要区别'在于nginx如何'解释'location后面的uri [2]. 这会使两者分别以'不同的方式'将请求'映射'到'服务器资源文件'上
复制
四 $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