原由:今天企业部署一个项目,用的nginx作的反向代理,配置以下:html
测试结果使人失望,IP:端口 能访问项目,域名:端口 也能访问 ,可是 域名/接口名 访问失败nginx
##########################################################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; } }
注意:web
nginx作反向代理时后端
location ~ ^/(test)/ { proxy_pass http://QA-web; include /usr/local/nginx/conf/vhosts/proxy.conf; }
中的test不能随便取,必须与后端访问接口请求接口名称一致。tomcat
缘由:client-->请求-->nginx-->proxy_pass-->upstream-->后端服务器项目(注意:问题就在这儿,nginx能把www.danny.com/test的请求发给后端服务器,可是,
若是后端代码没有这个test接口,就算请求到达了,后端如tomcat不知道这个请求是个什么玩意儿,会直接致使请求失败)服务器
因此,这个test名还真不能随便取,必须nginx
能识别成功转发请求,后端服务器项目也得能识别这个请求才行,缺一不可。测试
企业项目测试总结经验spa