无需修改代码解决IIS部署下的Mixed content混合内容问题
问题描述
- 环境:自研Web门户采用IIS部署,通过URL Rewrite将请求转发至
http://<server ip>:9994,已配置SSL证书实现HTTPS访问 - 异常:进入特定标签页时浏览器报错:Blocked loading mixed active content “http://
:4320/ping” - 原因推测:ReportService服务(端口4320)为运行Web应用的Jserver提供报表信息,但当前IIS Rewrite规则仅指向Jserver,导致页面加载时直接发起HTTP协议的4320端口请求,触发浏览器混合内容拦截机制
- 需求:不修改应用代码,通过代理等方式解决问题
当前IIS配置片段
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> </preCondition> </preConditions> </outboundRules> <rules> <clear /> <rule name="Redirect to https" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{HTTPS}" pattern="off" ignoreCase="true" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" /> </rule> <rule name="ReverseProxyInboundRule1" enabled="true" patternSyntax="ECMAScript" stopProcessing="true"> <match url="(.*)" negate="false" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="false" /> <action type="Rewrite" url="http://domainname:9994/{R:1}" appendQueryString="true" logRewrittenUrl="true" /> </rule> </rules> </rewrite> <directoryBrowse enabled="false" /> <httpRedirect enabled="false" destination="https://domainname/webplus" /> </system.webServer>
解决方案(无需修改应用代码)
方案1:新增反向代理+出站链接替换规则
需确保已安装IIS的URL Rewrite和**Application Request Routing (ARR)**模块,并开启ARR代理功能。
- 添加入站代理规则,将HTTPS路径转发到4320端口
在<rules>节点内新增规则:<rule name="ProxyReportService" enabled="true" stopProcessing="false"> <match url="^report-proxy/(.*)" /> <action type="Rewrite" url="http://<ip>:4320/{R:1}" appendQueryString="true" /> </rule> - 添加出站规则,替换页面中硬编码的HTTP链接
在<outboundRules>节点内完善规则:<outboundRules> <rule name="ReplaceReportServiceHTTPURL" preCondition="IsHTML"> <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="http://<ip>:4320/(.*)" /> <action type="Rewrite" value="https://{HTTP_HOST}/report-proxy/{R:1}" /> </rule> <preConditions> <preCondition name="IsHTML"> <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" /> </preCondition> </preConditions> </outboundRules>
方案2:用Rewrite Maps批量管理替换规则
如果页面中有多个指向4320端口的链接,可通过Rewrite Map统一配置:
- 在
<rewrite>节点下添加映射规则:<rewriteMaps> <rewriteMap name="ReportURLMap"> <add key="http://<ip>:4320/ping" value="https://{HTTP_HOST}/report-proxy/ping" /> <!-- 可添加其他需替换的4320端口链接 --> </rewriteMap> </rewriteMaps> - 修改出站规则调用映射:
<outboundRules> <rule name="BatchReplaceReportURLs" preCondition="IsHTML"> <match filterByTags="A, Area, Base, Form, Frame, Head, IFrame, Img, Input, Link, Script" pattern="(http://<ip>:4320/.*)" /> <action type="Rewrite" value="{RewriteMap:ReportURLMap:{R:1}}" /> </rule> </outboundRules>
验证步骤
- 重启IIS站点,清除浏览器缓存
- 访问目标标签页,检查控制台是否仍有混合内容报错
- 查看网络请求,确认
/report-proxy/ping通过HTTPS发送,且被IIS转发至http://<ip>:4320/ping
内容的提问来源于stack exchange,提问作者spr
相关产品推荐
相关产品推荐

