栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx关于location和proxy使用总结

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx关于location和proxy使用总结

  在nginx中配置proxy_pass时,如果在proxy_pass后面的url加/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分给代理走。 

下面五种情况分别用http://127.0.0.1:84/abc/index.html进行访问。

配置完整信息(只是展示一个样例,全部内容和和拼接的URL看后面文章)

server
{
   listen 83;
   server_name 127.0.0.1;
   root /etc/nginx/conf.d/demo;
}

server
{
   listen 84;
   server_name rdtest.youzhanjia.com;
   location  /abc
    {
        proxy_pass http://127.0.0.1:83/linux;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

其中/etc/nginx/conf.d/demo路径下的index.html内容如下

浏览器访问:http://127.0.0.1:84/abc/index.html

测试结果:

//----------端口后不带/字符,表示衔接location全部URI路径

情况一:
location  /abc
proxy_pass http://127.0.0.1:83;

页面结果:abc index      

url拼接结果:http://127.0.0.1:83/abc/index.html

//----------端口后带/字符,替换location指定的字符

情况二:
location  /abc
proxy_pass http://127.0.0.1:83/;

页面结果:home index    

url拼接结果:http://127.0.0.1:83/index.html

情况三:

location  /abc
proxy_pass http://127.0.0.1:83/linux/;

页面结果:linux index    

url拼接结果:http://127.0.0.1:83/linux//index.html

情况四:

location  /abc
proxy_pass http://127.0.0.1:83/linux;

页面结果:linux index    

url拼接结果:http://127.0.0.1:83/linux/index.html

情况五:

location  /abc/
proxy_pass http://127.0.0.1:83/linux;

页面结果:404 not found   

url拼接结果:http://127.0.0.1:83/linuxindex.html(体现了是字符替换的结果,源码没看,只是猜测,URL是根据nginx错误日志发现的路径)

情况六:

location  /abc/
proxy_pass http://127.0.0.1:83/linux/;

页面结果:linux index    

url拼接结果:http://127.0.0.1:83/linux/index.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/422603.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号