2021年8月

首先需求是这样的:需要根据用户输入的域名或IP(内网或外网)来加载不同的静态资源。

之前功能是好的,最近使用了iis做反向代理发现获取到的是服务器的IP,排查了好久才找到原因出在反向代理上:

  • 代理为了提高性能,一些Http头部信息不回转发给后台服务器,其中就包括代理服务器的host信息,而tomcat中对于request.getServerName()的实现,就是取这个host信息,如果http
    header总没设置,则取应用所在服务器IP地址。

所以,需要设置下让代理服务器把Host转发给后台服务器。网上类似的资料都是apache和nginx的解决方法,iis的资料很少,搜了好久才找到,这里总结下:

1、apache:在<VirtualHost/>标签中的最后添加 ProxyPreserveHost on

<VirtualHost *:80>
    RewriteEngine on
    ProxyPass /TLimages/ !
    ProxyPass /imagelist/ !
    ProxyPass /xiazai/ !
    ProxyPass /ad/ !
    ProxyPass / balancer://proxy/
    ProxyPassReverse / balancer://proxy/
    ProxyPreserveHost on
</VirtualHost>

2、nginx:在location {…}中添加 proxy_set_header Host $host

location ^~/proxy_path/ {
    root "/www/html";
    index index.html;
    proxy_pass http://192.168.223.137/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

3、iis:设置preserveHostHeader:true

%windir%\system32\inetsrv\appcm d.exe set config -section:system.webServer/proxy -preserveHostHeader:true /commit:apphost

相关文章:http://www.yyjjssnn.cn/articles/842.html