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... 

 总结:

Nginx 的 proxy_pass 行为取决于是否包含尾部斜杠:
  1. 无尾部斜杠(如 proxy_pass http://192.168.3.150:8500;):保留客户端请求的完整路径。
  2. 有尾部斜杠(如 proxy_pass http://192.168.3.150:8500/;):会剥离 location 匹配的前缀。
posted @ 2019-01-18 16:03  叮伱格斐呃  阅读(1447)  评论(0)    收藏  举报
Live2D