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 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 ~ ^/(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
能识别成功转发请求,后端服务器项目也得能识别这个请求才行,缺一不可。

企业项目测试总结经验

原文地址:https://www.cnblogs.com/dannylinux/p/10288140.html