温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-06-26 20:52:13 来源:网络 阅读:6980 作者:独指蜗牛 栏目:建站服务器

假设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时,匹配到该区块,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。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI