小知识: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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 不行

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
root@pts/1 $ curl http://tapi.xxxx.com/lctest/
hello world
root@pts/1 $ curl http://tapi.xxxx.com/lctest2/
hello world
2
root@pts/1 $ curl http://tapi.xxxx.com/lctest3/
3
hello world
root@pts/1 $ curl http://tapi.xxxx.com/lctest4/
hello world
4

location alias

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
location /lctest5 {
alias /data/web/;
}
location /lctest6/ {
alias /data/web/;
}
location /lctest7 {
alias /data/web;
}
## 403 /data/web forbidden
location /lctest8/ {
alias /data/web;
}

curl 测试结果如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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/
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor=”white”>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

location proxy_pass

?
1
2
3
4
5
6
7
8
9
#——–proxy_pass配置———————
location /t1/ { proxy_pass http://servers; }  #正常,不截断
location /t2/ { proxy_pass http://servers/; }  #正常,截断
location /t3 { proxy_pass http://servers; }  #正常,不截断
location /t4 { proxy_pass http://servers/; }  #正常,截断
location /t5/ { proxy_pass http://servers/test/; }  #正常,截断
location /t6/ { proxy_pass http://servers/test; }  #缺”/”,截断
location /t7 { proxy_pass http://servers/test/; }  #含”//”,截断
location /t8 { proxy_pass http://servers/test; }  #正常,截断

测试脚本

?
1
2
3
4
5
6
for i in $(seq 8)
do
url=http://tapi.xxxx.com/t$i/doc/index.html
echo “———–$url———–“
curl url
done

测试结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
———-http://tapi.xxxx.com/t1/doc/index.html————
/t1/doc/index.html
———-http://tapi.xxxx.com/t2/doc/index.html————
/doc/index.html
———-http://tapi.xxxx.com/t3/doc/index.html————
/t3/doc/index.html
———-http://tapi.xxxx.com/t4/doc/index.html————
/doc/index.html
———-http://tapi.xxxx.com/t5/doc/index.html————
/test/doc/index.html
———-http://tapi.xxxx.com/t6/doc/index.html————
/testdoc/index.html
———-http://tapi.xxxx.com/t7/doc/index.html————
/test//doc/index.html
———-http://tapi.xxxx.com/t8/doc/index.html————
/test/doc/index.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/849a6c068daa

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Linux tomcat下catalina.out日志文件分割

2023-4-6 4:00:26

建站知识

小知识:Linux驱动实践:中断处理中的【工作队列】 Workqueue 是什么鬼?

2023-4-6 4:08:40

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索