nginx反向代理proxy_pass的问题及小总计

 

起因:今天企业部署一个项目,用的nginx做的反向代理,配置如下:

测试结果令人失望,IP:端口 能访问项目,域名:端口 也能访问 ,但是 域名/接口名 访问失败

 

复制代码
##########################################################proxy_cache########################################################
proxy_cache_path      /opt/proxy_temp levels=1:2 keys_zone=imgcache:100m inactive=30d max_size=50g;


upstream danny-web {
         ip_hash;
         server 192.168.1.150:7088;
         server 192.168.1.151:7088;
         check interval=3000 rise=2 fall=5 timeout=1000 default_down=true type=http;
        }


upstream user-web {
         ip_hash;
         server 192.168.1.150:7068;
         server 192.168.1.151:7068;
         check interval=3000 rise=2 fall=5 timeout=1000 default_down=true type=http;
        }


upstream QA-web {
         ip_hash;
         server 192.168.1.150:7091;
         server 192.168.1.151:7091;
         check interval=3000 rise=2 fall=5 timeout=1000 default_down=true type=http;
        }


server {
         listen       80;
         server_name  danny.com;
         location ^~ /WEB-INF {
           deny all;
         }

        ##upstream status
        location /upstream_status {
          allow 192.168.0.0/16;
          deny all;
          check_status;
          access_log off;
        }

        ##nginx status
          deny all;
          stub_status on;
          access_log off;
        }

        location / {
          proxy_pass http://danny-web;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }

        location ~ ^/(v(\d+)/user|v(\d+)/code|v(\d+)/partners)/ {
          proxy_pass http://user-web;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }

        location /danny/app/ {
          proxy_pass http://192.168.3.150:8500;
          include /usr/local/nginx/conf/vhosts/proxy.conf;

        }

        location ~ ^/(test)/ {
          proxy_pass http://QA-web;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }

        error_page   403 500 502 503 504  /50x.html;
        location  /50x.html {
            root   html;
        }

}
复制代码

 

 

注意:

nginx做反向代理时

    location ~ ^/(test)/ {
          proxy_pass http://QA-web;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }

中的test匹配名称必须与后端访问接口名称一致

 

原因:client-->请求-->nginx-->proxy_pass-->upstream-->后端服务器项目(注意:问题就在这儿,nginx能把www.danny.com/test的请求发给后端服务器,但是,
如果后端代码没有这个test接口,就算请求到达了,后端如tomcat不知道这个请求是个什么玩意儿,会直接导致请求失败)

所以,这个test名不能随便取,必须nginx
能识别成功转发请求,后端服务器项目也得能识别这个请求才行,缺一不可。

示例图如下:接口以test开头即可匹配上

 

 

补充nginx转发代理小总结:

location
1.等号类型(=)的优先级最高,需要精确匹配。一旦匹配成功,则不再查找其他匹配项。

2.^~类型表达式一旦匹配成功,则不再查找其他匹配项。

3.正则表达式类型(~~”)的优先级次之。

4.如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。

(location =)>(location 完整路径)>(location ^~ 路径)>(location~,~*正则顺序)>(location 部分起始路径)>(/)

 

proxy_pass 路径带/的问题

例:用户访问的 url 为 http://danny.com/danny/app/xxx...

复制代码
#1.proxy_pass不带"/" (常用)
        location /danny/app/ {
          proxy_pass http://192.168.3.150:8500;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }
 
#访问代理路径为:http://192.168.3.150:8500/danny/app/xxx...   


#2.proxy_pass有"/"
        location /danny/app/ {
          proxy_pass http://192.168.3.150:8500/;
          include /usr/local/nginx/conf/vhosts/proxy.conf;
        }

#访问代理路径为:http://192.168.3.150:8500/xxx... 
复制代码

 

posted @   叮伱格斐呃  阅读(1425)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2018-01-18 python自动化运维笔记1 —— 系统性能信息模块psutil
Live2D
欢迎阅读『nginx反向代理proxy_pass的问题及小总计』
点击右上角即可分享
微信分享提示