proxy_set_header

描述:瀏覽器訪問nginx(http://10.90.3.118:80),nginx又將請求代理到了後端的springboot(http://10.90.3.119:8080),springboot中須要作302跳轉,怎麼讓瀏覽器發起的跳轉指向http://10.90.3.118:80。java

若是直接在Java中這樣獲取,拿到的是springboot服務器的IP和端口:nginx

String host = request.getServerName(); //10.90.3.119
String port = request.getServerPort(); //8080

//另外
String remoteHost = request.getRemoteHost();//10.90.3.118
String remotePort = request.getRemotePort();//44638

關於remoteHost、remotePort這裏不討論。 可見host、port是springboot服務器ip和端口。那麼如何獲取nginx的ip和端口呢?spring

能夠在配置代理時添加 proxy_set_header 配置項:後端

location ^~ /api {
     proxy_pass http://10.90.3.119:8080;
     proxy_set_header Host $host;
 }

再次獲取host、port,就能獲取成功了:api

String host = request.getServerName(); //10.90.3.118
String port = request.getServerPort(); //80
本站公眾號
   歡迎關注本站公眾號,獲取更多信息