新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

nginx虚拟路径中proxy_pass对后端请求的影响

假设nginx中的配置是这样的:

10余年专注成都网站制作,企业网站设计,个人网站制作服务,为大家分享网站制作知识、方案,网站设计流程、步骤,成功服务上千家企业。为您提供网站建设,网站制作,网页设计及定制高端网站建设服务,专注于企业网站设计,高端网页制作,对报废汽车回收等多个方面,拥有丰富的网站维护经验。

server {

    listen 80;

    server_name x.x.x.x;

    . . . . . .

    location /subdir

    {

        proxy_pass http://y.y.y.y;

    }

}

那么,当用户请求http://x.x.x.x/subdir/other时,匹配到该区块,nginx反向代理到后端时会保留虚拟路径。nginx实际向后端发起的请求URL为http://y.y.y.y/subdir/other。

 

假设nginx中的配置是这样的:

server {

    listen 80;

    server_name x.x.x.x;

    . . . . . .

    location /subdir

    {

        proxy_pass http://y.y.y.y/;

    }

}

那么,当用户请求http://x.x.x.x/subdir/other时,匹配到该区块,由于proxy_pass中有指定后端URI路径,nginx代理请求到后端时不会保留虚拟路径。nginx实际向后端发起的请求URL为http://y.y.y.y//other。

 

假设nginx中的配置是这样的:

server {

    listen 80;

   server_name x.x.x.x;

   . . . . . .

    location /subdir

    {

       proxy_pass http://y.y.y.y/upstream_subdir;

   }

}

那么,当用户请求http://x.x.x.x/subdir/other时,匹配到该区块,由于proxy_pass中有指定后端URI路径,nginx代理请求到后端时不会保留虚拟路径。nginx实际向后端发起的请求URL为http://y.y.y.y/upstream_subdir/other。

 

综合上述三个示例可以发现,问题的关键就在于proxy_pass指令中是否有指定后端服务器的URI,这决定了nginx代理请求到后端时是否会保留location中的虚拟路径。

 

当将后端服务器写成upstream方式时,效果也是一样的。比如:

upstream backend {

    server y.y.y.y:80;

}

server {

   listen 80;

   server_name x.x.x.x;

   . . . . . .

    location /subdir

    {

       proxy_pass http://backend/upstream_subdir;

   }

}

当用户请求http://x.x.x.x/subdir/other时,nginx实际向后端发起的请求URL仍然为http://y.y.y.y/upstream_subdir/other。


当前名称:nginx虚拟路径中proxy_pass对后端请求的影响
本文网址:http://www.wjwzjz.com/article/iehscd.html
在线咨询
服务热线
服务热线:028-86922220
TOP