nginx中proxy_pass的使用(alias和root使用)

前面我们一起学习了location的匹配规则,如果还不了解的话可以参考我这边文章(nginx中location的使用),今天一起来学习nginx中proxy_pass的匹配过程,也是非常简单

proxy_pass匹配主要分两种情况

1、proxy_pass代理的url后面只有ip(域名)+端口,其他什么都没有(包括"/"都不能有)

此时代理的路径需要把请求的url中ip+port后面的路径追加到proxy_pass后面

例如:

假设http的请求路径为:http://123.25.95.148:9998/nginx/hello?name=taolong

nginx配置文件内容

server {
        listen       9998;
        server_name     123.25.95.148;
    
     	#匹配规则
        location /nginx {
               proxy_pass http://123.25.95.148:10010;
        }
   

}

此时

proxy_pass后面的url=http://123.25.95.148:10010,没有任何内容

这是就需要将http请求路径中的 “nginx/hello?name=taolong”内容追加到proxy_pass的url后面

最终代理的路径为:http://123.25.95.148:10010/nginx/hello?name=taolong

2、proxy_pass代理的url后面除了ip(域名)+端口,还有其他的内容

此时的匹配逻辑,就需要将请求中的未匹配到location的内容追加到proxy_pass的url后面

例如:

假设http的请求路径为:http://123.25.95.148:9998/nginx/hello?name=taolong

nginx配置文件的内容如下:

server {
        listen       9998;
        server_name     123.25.95.148;
    

     	#匹配规则
        location /nginx/hello {
        	   #注意这里是“/”结尾,请求url中未匹配的内容:?name=taolong
               proxy_pass http://123.25.95.148:10010/hello;
        }
        #此时上面输出的结果:http://123.25.95.148:10010/hello?name=taolong
	
		#匹配规则
        location /nginx {
        	   #注意这里是“/”结尾,请求url中未匹配的内容:/hello?name=taolong
               proxy_pass http://123.25.95.148:10010/;
        }
        #此时上面输出的结果:http://123.25.95.148:10010/hello?name=taolong
}

proxy_pass就到上面就结束了,下面顺带提一下nginx还有一种类似上面的情况,就是root和alias的使用

root和alias使用

当使用root时,就类似上面第一种情况,直接对应到root指定的目录

当使用alias时,就类似上面的第二种情况,将为匹配的内容追加到alias的url后面

		#测试路径:/root
        #定位的内容:/etc/nginx/html/root/a.html;
        location /root {
                root /etc/nginx/html;
                index a.html;
        }


        #测试路径:/root/test
        #定位的内容:/etc/nginx/html/root/test/b.html;
        location /root/test {
                root /etc/nginx/html;
                index a.html;
        }


        #测试路径:/alias/test/a
        #定位的内容:/etc/nginx/html/test/a/b.html
        location /alias {
                alias /etc/nginx/html;
                index b.html;
        }


        #测试路径:/alias/test/
        #定位的内容:/etc/nginx/html/a.html
        location /alias/test {
                alias /etc/nginx/html;
                index a.html;
        }
·		#正则$1表示第一次匹配的路径变量对应匹配的.*的内容
        #测试路径/aliasregex/test   ---》定位的内容/etc/nginx/html/test/a.html
        #测试路径/aliasregex/a   ---》定位的内容/etc/nginx/html/a/a.html
        location ~ /aliasregex/(.*) {
            alias /etc/nginx/html/$1;
            index a.html;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值