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
本站公众号
   欢迎关注本站公众号,获取更多信息