【问题标题】:IIS Redirect HTTP to HTTPS for same domain names on the same IISIIS 将同一 IIS 上相同域名的 HTTP 重定向到 HTTPS
【发布时间】:2020-08-16 19:02:24
【问题描述】:

我有一个特殊情况: 在同一个 IIS 上有网站:

  1. sub.domain.com - 它在 80 上有 http,在 443 上有 https。它非常适合您从 http 到 https 的重定向方式

         <configuration>
             <system.webServer>
                 <rewrite>
                     <rules>
                         <rule name="HTTPS force" enabled="true" stopProcessing="true">
                             <match url="(.*)" />
                             <conditions>
                                <add input="{HTTPS}" pattern="^OFF$" />
                             </conditions>
                             <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" 
                redirectType="Permanent" />
                       </rule>
                   </rules>
                </rewrite>
             </system.webServer>
      </configuration>
    
  2. http://sub.domain.com:5000 和 https//sub.domain.com:5001。那个只是拒绝从http传递到https到正确的端口。它可以从http://sub.domain.com:5000 指向https://sub.domain.com:5000 而不是https://sub.domain.com:5001。除非我们清除浏览数据,否则它只能从http://sub.domain.com:5000https://sub.domain.com:5001 工作一次。规则喜欢

         <rule name="HTTP to HTTPS on different SSL Port" enabled="true" stopProcessing="true">
        <match url="(.*)" ignoreCase="true" />
           <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
               <add input="{HTTPS}" pattern="off" />
               <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
           </conditions>
        <action type="Redirect" url="https://{C:1}:5001/{R:0}" appendQueryString="false" />
     </rule>
    

所以我的问题是如何在不清除浏览数据等的情况下成功地将http://sub.domain.com:5000 重定向到https://sub.domain.com:5001

更新:

在我记录失败的请求日志后,我发现了一些东西。 我在FailedReqLogFiles\W3SVC4 中有 18 个文件。它们每个都有不同的 requestURL。

  1. 第一个是http://somedomain.com:5000/,状态码301;
  2. 第二个是https://somedomain.com:5001/,状态码200。
  3. 第三个是https://somedomain.com:5001/inline.e576fger98965b3219.bundle.js,状态码200。
  4. 第四个是https://somedomain.com:5001/polyfills.3bed24531fd9085545fdw.bundle.js,状态码200。
  5. 等等……
  6. 最后一位(第 18 位)是https://somedomain.com:5001/assets/images/action,jpg

如果我查看请求详细信息选项卡中的 GENERAL_ENDPOINT_INFORMATION 节点。它在每个日志文件中都显示Failed To Complete

重要提示:

这是 .net 核心加 Angular 应用程序。我不确定我应该在角度侧而不是 IIS 或两侧配置 ssl 和重定向 https?

【问题讨论】:

  • 你有Strict-Transport-Security http响应头吗?是的,然后它可以解释为什么它在缓存清除后一次起作用。 en.wikipedia.org/wiki/HTTP_Strict_Transport_Security
  • @StanislavBerkov,我有。 Strict-Transport-Security: max-age=2592000
  • 但是在角码http头设置中,我没有这个。我应该在Angular中添加它吗?
  • 我是说 Strict-Transport-Security 可能是问题的根本原因。尝试删除它。
  • @StanislavBerkov,我不知道如何删除它。在 IIS 中,我不会将它添加到响应标头中。响应头只包含X-Powered-By: ASP.NET

标签: angular redirect .net-core https iis-8.5


【解决方案1】:

您可以尝试以下规则:

 <rule name="HTTP to HTTPS on different SSL Port" enabled="true" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
                    <add input="{HTTPS}" pattern="off" />
                    <add input="{SERVER_PORT}" pattern="^2000$" />
                    <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
                </conditions>
                <action type="Redirect" url="https://{C:1}:2001/{R:1}" appendQueryString="false" />
            </rule>

注意:根据您的站点绑定和要求以模式设置端口号。

另外,尝试设置 iis 客户端缓存设置。

https://docs.microsoft.com/en-us/iis/configuration/system.webserver/staticcontent/clientcache

【讨论】:

  • 我认为您的规则仅适用于在 IIS 上只有唯一域名的情况。如果您有另一个具有相同功能的站点,则可能无法正常工作。请参阅stackoverflow.com/questions/61890199/…。该答案中的 cmets 可能有用。请验证。谢谢。
  • 我添加了失败的请求跟踪。我只收到REWRITE_DISABLED_KERNEL_CACHE 警告。不确定是否相关。
  • @Bigeyes 您能否分享失败的请求跟踪日志,如图所示。 image。 HTTP 和 https 的同一站点的域名是可以的。但端口号应该不同。如果可能,建议使用 https 443 的默认端口。
  • @Bigeyes 您能否分享更多详细信息,您所说的“如果您有另一个具有相同的站点,也许它不起作用。”您是否在不同站点使用相同的域名?
  • 我的意思是如果你有http://www.somedomain.com:80https://www.somedomain.com:443,可以将http重定向到https。但在我的情况下,在同一个 IIS 上。我正在开发具有相同域名和不同端口的新站点,以区分现有站点。所以我有http://www.somedomain.com:5000https://www.somedomain.com:5001 的网站。换句话说,我为不同的站点拥有相同的域名。重定向不适用于开发中的站点,但适用于现有站点。除非我们每次都清除浏览数据。
【解决方案2】:

经过几天的挣扎。终于找到解决办法了。

我必须做两件事。

  1. 在 IIS 上应用规则

    <rule name="HTTP to HTTPS on different SSL Port" enabled="true" stopProcessing="true">
    <match url="(.*)" ignoreCase="true" />
       <conditions logicalGrouping="MatchAll" trackAllCaptures="true">
           <add input="{HTTPS}" pattern="off" />
           <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
       </conditions>
    <action type="Redirect" url="https://{C:1}:5001/{R:0}" appendQueryString="false" />
    
  2. 从 .net 核心代码中删除 HSTS。我有代码

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
           app.UseHsts();
    

删除它然后工作。感谢@StanislavBerkov

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 2013-03-23
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2013-05-02
    • 2018-12-06
    • 2018-01-18
    相关资源
    最近更新 更多