1 root和alias
注意:root则是最上层目录的定义,alias是一个目录别名的定义。
1.1 alias用法
映射路径是对上层路径的替换。
location /img/ {
alias /home/static/img/;
}
当请求“/img/test.png
”时,映射地址为”/home/static/img/test.png
“=“/home/static/img/
”+“test.png
”。
location /img {
alias /home/static/img;
}
当请求“/img/test.png
”时,映射地址为”/home/static/img/test.png
“=“/home/static/img
”+“/test.png
”。
1.2 root用法
映射路径是对上层路径的拼接。
# 下面的结果跟上面的结果一样
location /img/ {
root /home/static/img/;
}
当请求“/img/test.png
”时,映射地址为”/home/static/img//img/test.png
“=“/home/static/img/
”+“/img/test.png
”,注意此处的”//“nginx应该做了处理,请求时对请求结果没有影响。
location /img {
root /home/static/img;
}
当请求“/img/test.png
”时,映射地址为”/home/static/img/img/test.png
“=“/home/static/img
”+“/img/test.png
”。
2 proxy_pass的斜杠
proxy_pass的”/“对资源请求影响很大。
2.1 资源替换
只要”http://192.168.2.10:5000
“含有URI(包括/
),就属于资源替换。
location /api/ {
alias http://192.168.2.10:5000/api/;
}
当请求是"http://localhost/api/index
"时,http://localhost/api/
替换为http://192.168.2.10:5000/api
,代理地址是“http://192.168.2.10:5000/api/index
”=“http://192.168.2.10:5000/api/
”+“index
”。
location /api/ {
alias http://192.168.2.10:5000/;
}
当请求是"http://localhost/api/index
"时,,http://localhost/api/
替换为http://192.168.2.10:5000/
,代理地址是“http://192.168.2.10:5000/index
”=“http://192.168.2.10:5000/
”+“index
”。
location /api {
alias http://192.168.2.10:5000/;
}
当请求是"http://localhost/api/index
"时,http://localhost/api
替换为http://192.168.2.10:5000/
,代理地址是“http://192.168.2.10:5000/index
”=“http://192.168.2.10:5000/
”+“/index
”。注意,此处还有“//
”。
location /api/ {
alias http://192.168.2.10:5000/api;
}
当请求是"http://localhost/api/index
"时,"http://localhost/api/
“替换为"http://192.168.2.10:5000/api
”,代理地址是“http://192.168.2.10:5000/apiindex
”=“http://192.168.2.10:5000/api
”+“index
”。
2.2 资源拼接
只要"http://192.168.2.10:5000
"后面不含有URI,就是属于资源拼接。
location /api {
alias http://192.168.2.10:5000;
}
当请求是"http://localhost/api/index
"时,代理地址是“http://192.168.2.10:5000/api/index
”=“http://192.168.2.10:5000
”+“/api/index
”。
location /api/ {
alias http://192.168.2.10:5000;
}
当请求是"http://localhost/api/index
"时,代理地址是“http://192.168.2.10:5000/api/index
”=“http://192.168.2.10:5000
”+“/api/index
”。