nginx代理的几种场景分析

本文深入解析Nginx在不同代理配置下对URL的处理方式,包括如何代理到目标路径,以及隐性和显性地址跳转的实现方法。特别关注了基于user-agent的条件重写规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx中对/的处理方式不同
我们访问路径:http://localhost/proxy/test.html 进行分析:

场景1:
location /proxy/ {
   proxy_pass http://loalhost/;
}

代理到http://localhost/test.html

场景2:
location /proxy/ {
   proxy_pass http://localhost;
}

代理到http://localhost/proxy/test.html

场景3:
location /proxy/ {
   proxy_pass http://localhost/home/;
}

代理到http://localhost/home/test.html

场景4:
location /proxy/ {
   proxy_pass http://localhost/home;
}

代理到http://localhost/hometest.html

其他

1.隐性地址跳转(通过user-agent判断ios和android)

location /article/html/tutorial.html {
    if ( $http_user_agent ~* "iPhone" ){
       rewrite ^/article/html/tutorial.html$ /article/html/tutorial_ios.html last; 
       break;
    } 
    root /Users/hecj/workspace/dist;
}

注:上面这个不会改变地址栏url

2.显性地址跳转

location /article/html/tutorial.html {
     if ( $http_user_agent ~* "iPhone" ){
        rewrite ^(.*) http://test.com/article/html/tutorial_ios.html permanent; 
        break;
      } 
      root /Users/hecj/workspace/dist;
}

注:上面这个会改变地址栏url

3.隐性地址跳转

   location /renrendict.html {
        proxy_pass http://test.com/html/download/renrendict.html;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值