默认情况下,apache或Nginx作为代理为了提高性能,一些Http头部信息不回转发给后台服务器,其中就包括代理服务器的host信息,而tomcat中对于request.getServerName() 的实现,就是取这个host信息,如果http header总没设置,则取本机IP(127.0.0.1)地址。
- apache :在标签最后添加
ProxyPreserveHost on
- nginx:在location {…}中添加
proxy_set_header Host $host;
location / { proxy_pass http://127.0.0.1:18080/; proxy_set_header Host $host; # proxy_set_header Host $host:$server_port; #服务器名称和端口一起通过代理服务器传递 }