nginx location中uri的截取的实现方法
说明:
location中的root和alias
- root指令只是将搜索的根设置为root设定的目录,即不会截断uri,而是使用原始uri跳转该目录下查找文件
 - aias指令则会截断匹配的uri,然后使用alias设定的路径加上剩余的uri作为子路径进行查找
 
location中的proxy_pass的uri
如果proxy_pass的url不带uri
- 如果尾部是"/",则会截断匹配的uri
 - 如果尾部不是"/",则不会截断匹配的uri
 
如果proxy_pass的url带uri,则会截断匹配的uri
Examples
location中的root
root@pts/1$ls-ld/data/web/lctest*|awk'{print$NF}'
/data/web/lctest
/data/web/lctest2
/data/web/lctest3
/data/web/lctest4
location/lctest{
root/data/web/;
}
location/lctest2/{
root/data/web/;
}
location/lctest3{
root/data/web;
}
location/lctest4/{
root/data/web;
}
curl测试结果如下
备注:浏览器输入的时候最后面不添加/,会自动补上,但是curl不行
root@pts/1$curlhttp://tapi.xxxx.com/lctest/ helloworld root@pts/1$curlhttp://tapi.xxxx.com/lctest2/ helloworld 2 root@pts/1$curlhttp://tapi.xxxx.com/lctest3/ 3 helloworld root@pts/1$curlhttp://tapi.xxxx.com/lctest4/ helloworld 4
locationalias
location/lctest5{
alias/data/web/;
}
location/lctest6/{
alias/data/web/;
}
location/lctest7{
alias/data/web;
}
##403/data/webforbidden
location/lctest8/{
alias/data/web;
}
curl测试结果如下
curl'http://tapi.kaishustory.com/lctest5/' curl'http://tapi.kaishustory.com/lctest6/' curl'http://tapi.kaishustory.com/lctest7/' 结果都是/data/web/index.html的输出 root@pts/1$curl'http://tapi.kaishustory.com/lctest8/'403Forbidden 403Forbidden
nginx