对于同一IIS上的相同域名,IIS将HTTP重定向到HTTPS

问题描述

我有一个特殊情况: 同一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:5000https://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. 最后一个(18th)是https://somedomain.com:5001/assets/images/action,jpg

如果我在“请求详细信息”选项卡中查看GENERAL_ENDPOINT_@R_791_4045@ION节点。它将在每个日志文件显示Failed To Complete

重要提示

这是.net核心加上有角度的应用程序。我不确定我应该配置ssl并在有角度的一面而不是在IIS或两面都重定向https吗?

解决方法

您可以尝试以下规则:

 <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>

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

enter image description here

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

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

,

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

我必须做两件事。

  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