网站需要 SSL 时应用程序初始化模块失败

本文可帮助你解决应用程序初始化模块不适用于配置为需要安全套接字层(SSL)的网站的问题。

原始产品版本: ASP.NET
原始 KB 数: 2843964

现象

以前称为 应用程序预热 的应用程序初始化模块不适用于配置为需要 SSL 的网站。

原因

此为有意行为。

预热模块使用超文本传输协议(HTTP)而不是超文本传输协议安全(HTTPS)发送请求。 建议的解决方法将允许 HTTP 请求从预热模块到 localhost,但它会重定向到 HTTPS 以获取其余请求,因此,此处的设计意味着预热模块通过 HTTP 发出请求。

解决方法

若要解决此限制,可以考虑启用 HTTP(未选中 IIS 管理器>SSL 设置下的“要求 SSL 设置”),并使用 URL 重写规则将 HTTP 请求重定向到 HTTPS,但请求来自预热模块的请求除外:

<rewrite>
    <rules>
        <rule name="No redirect on warmup request (request from localhost with warmup user agent)"
        stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="localhost" />
                <add input="{HTTP_USER_AGENT}" pattern="Initialization" />
            </conditions>
            <action type="Rewrite" url="{URL}" />
        </rule>
        <rule name="HTTP to HTTPS redirect for all requests" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
                <add input="{HTTPS}" pattern="off" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
    </rules>
</rewrite>