iis < serverVariables>断开< set name="..."/> Web配置

l7mqbcuq  于 2023-01-21  发布在  其他
关注(0)|答案(2)|浏览(135)

有TL;问题底部的DR!

开始了...
这个问题的背景(简单地说)是IIS重写在重定向操作中丢弃了协议。因此,如果您没有在url="..."中显式指定协议,<action type="Redirect" url="whatever"/>的规则将从https变为http,例如url="https://{HOST}"
在Google上搜索了很多之后,我找到了**this article**,它描述了多种在重定向时保持协议的方法,最适合我的方法如下:

<rule name="Create HTTP_PROTOCOL">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{CACHE_URL}" pattern="^(.+)://" />
    </conditions>
    <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" />
    </serverVariables>
    <action type="None" />
</rule>

<rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^localtest\.me$" />
    </conditions>
    <action type="Redirect" url="{HTTP_PROTOCOL}://www.localtest.me/{R:1}" />
</rule>

......之所以如此,有两个原因:
1.有多个规则需要维护协议。2每个规则写两个(一个只用于http,另一个用于https)是不可行的。
1.此文件必须在直接SSL连接(其中{HTTP}=on)上工作,并与Cloudflare的灵活SSL兼容(其中{HTTP}=off,但{HTTP_X-Forwarded-Proto}为“http”或“https”)。
所以,
我们的想法是设置一个变量,我可以用它来控制rewriteMap,这个变量的值取决于HTTPS=onHTTP_X-Forwarded-Proto=https
下面是我的原始代码,它删除了协议:

<!-- remove trailing slashes looses protocol -->
<rule name="RemoveTrailingSlashRule" stopProcessing="true">
    <match url="(.*)/+$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" />
</rule>

下面是我修改过的代码,它使用所需的协议重定向,无论是直接SSL连接还是通过CloudFlare的Flexible SSL连接:

<!-- rewrite config -->
<rewrite>

  <!-- rewrite maps -->
  <rewriteMaps>
      <rewriteMap name="RedirectBase">
          <add key="http" value="http://{HTTP_HOST}/" />
          <add key="https" value="https://{HTTP_HOST}/" />
      </rewriteMap>
  </rewriteMaps>

  <!-- rewrite rules -->
  <rules>

    <!-- capture incoming protocol -->
    <rule name="HTTP_PROTOCOL - Capture Default">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{CACHE_URL}" pattern="^(.+)://" />
      </conditions>
      <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" />
      </serverVariables>
      <action type="None" />
    </rule>

    <!-- overwrite protocol var if using CloudFlare's flexible SSL -->
    <rule name="HTTP_PROTOCOL - Overwrite with CloudFlare header">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{HTTP_X-Forwarded-Proto}" pattern="^https$" />
      </conditions>
      <serverVariables>
        <set name="HTTP_PROTOCOL" value="{C:1}" replace="true" />
      </serverVariables>
      <action type="None" />
    </rule>

    <!-- remove trailing slashes but keep protocol -->
    <rule name="RemoveTrailingSlashRule" stopProcessing="true">
        <match url="(.*)/+$" />
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
        <action type="Redirect" url="{RedirectBase:{HTTP_PROTOCOL}}{R:1}" />
    </rule>

  </rules>
</rewrite>

只是
A)我得到一个空白页面,url重写失败,没有错误信息。
B)但是如果我删除这个标记的两个示例,它就可以工作:

TL;DR为什么不起作用?

<serverVariables>
    <set name="HTTP_PROTOCOL" value="something" />
  </serverVariables>
qv7cva1a

qv7cva1a1#

找到答案了!
我不得不手动添加“HTTP_PROTOCOL”到URL重写配置中允许的服务器变量列表中。这似乎只能通过IIS用户界面完成,而不能通过web.config文件。Full instructions here

h4cxqtbf

h4cxqtbf2#

似乎服务器变量是"服务器"配置,而不是"Web"配置。
参见https://social.msdn.microsoft.com/Forums/en-US/755416cd-9085-4158-bf34-8593904edfa4/how-can-i-define-a-server-variable-in-webconfig?forum=iisurlrewritemodule
当您从IIS管理器添加服务器变量时,默认情况下会写入applicationHost.config文件。它位于%systemdrive%\Windows\System32\inetsrv\config。请检查服务器变量是否在此正确定义。
通过将以下条目添加到applicationHost.config文件中,可以手动定义服务器变量:

<location path="your site name">
    <system.webServer>
        <rewrite>
            <allowedServerVariables>
                <add name="server variable name" />
            </allowedServerVariables>
        </rewrite>
    </system.webServer>
</location>

相关问题