nginx中资源配置参数

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”。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值