在IIS 7/8上配置HSTS

发布时间:2017/5/22 18:27:46 打印 字号:

从IIS 7开始,可以在IIS上配置HSTS

考虑到HSTS实现主要由特定的头部组成,可选择重定向,有多种方法来配置IIS的HSTS。

通过GUI

注意,不可能在头文件上写入条件。HSTS规范清楚地表明,只需要在HTTS上提供HSTS头,而不是在HTTP上。

添加一个新的头:

  • 运行IIS管理器。

  • 选择你的网站

  • 选择HTTP REsponse头。

  • 单击操作部分中的添加。

  • 在“ 添加自定义HTTP响应头 ”对话框中,添加以下值:

    • 名称:严格运输安全

    • 价值:max-age = 15552001; includeSubDomains; 预紧

还建议将所有HTTP流量重定向到HTTPS。

通过手动IIS配置

您可以通过直接使用IIS配置,特别是system.webServer部分,更精细地配置头文件和重定向。

您可以将这些说明添加到您的system.webServer部分以添加必要的HSTS标题,并将HTTP重定向到HTTPS:


<?xml version="1.0" encoding="UTF-8"?>

    <configuration>

      <system.webServer>

        <rewrite>

          <rules>

            <rule name="HTTP to HTTPS redirect" stopProcessing="true">

              <match url="(.*)" />

              <conditions>

                <add input="{HTTPS}" pattern="off" ignoreCase="true" />

              </conditions>

              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />

             </rule>

           </rules>

           <outboundRules>

             <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">

               <match serverVariable="RESPONSE_Strict_Transport_Security" pattern=".*" />

               <conditions>

                 <add input="{HTTPS}" pattern="on" ignoreCase="true" />

               </conditions>

               <action type="Rewrite" value="max-age=31536000; includeSubDomains; preload" />

             </rule>

           </outboundRules>

         </rewrite>

       </system.webServer>

     </configuration>