原由:今天企業部署一個項目,用的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