首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >nginx中proxy_pass的使用(alias和root使用)

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

作者头像
全栈程序员站长
发布2022-07-04 14:51:44
发布2022-07-04 14:51:44
1.9K00
代码可运行
举报
运行总次数:0
代码可运行

前面我们一起学习了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配置文件内容

代码语言:javascript
代码运行次数:0
运行
复制
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配置文件的内容如下:

代码语言:javascript
代码运行次数:0
运行
复制
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后面

代码语言:javascript
代码运行次数:0
运行
复制
		#测试路径:/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;
        }

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/111214.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年8月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、proxy_pass代理的url后面只有ip(域名)+端口,其他什么都没有(包括”/”都不能有)
  • 2、proxy_pass代理的url后面除了ip(域名)+端口,还有其他的内容
  • root和alias使用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档