仅在使用 IIS ARR 代理时出现 HTTP 401 错误
HTTP 401 Error Only When Using IIS ARR Proxy
包含 Web 界面的应用程序是 运行 在安装了 IIS 10 和 ARR 的 Windows Server 2019 系统上。使用 IP:Port 导航到网页时,网页会正确加载。使用 domain.com 导航至该网页时,该网站的某些内容在控制台中显示 401 错误,并且页面无法正确加载。
当导航到域时,请求通过 IIS 和 URL 重写。似乎在通过代理传递信息方面存在一些问题。
这是 IIS 中重写规则的代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:33337/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:33337\/?(.*)" />
<action type="Rewrite" value="http{R:1}://sub.domain.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
</configuration>
这是针对此特定问题的 GitHub 问题 link:
https://github.com/qbittorrent/qBittorrent/issues/11207
我重现了这个问题。
显然 qBittorrent 期望客户端发送 same-origin Referer
headers。在您的情况下,它必须是 localhost:33337
但显然 sub.domain.com
正在发送。
此安全措施由 Enable Cross-Site Request Forgery (CSRF) protection
设置激活,可通过 qBitorrent > Options > Web UI > Security
访问。
您有两种解决方案。
- 禁用该设置。
- 用适当的值重写
Referer
header。
如果你想重写 header,在允许服务器变量 HTTP_REFERER
和 HTTP_ORIGIN
之后,就像你对 HTTP_ACCEPT_ENCODING
所做的那样,你应该改变你的规则如下。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:33337/{R:1}" />
<!-- New Optional Condition -->
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_REFERER}" pattern="^(?:https?://[^/]*/(.*))?$" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
<!-- New Header Rewrite -->
<set name="HTTP_REFERER" value="http://localhost:33337/{C:1}" />
<!-- Remove Origin Header -->
<set name="HTTP_ORIGIN" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:33337\/?(.*)" />
<action type="Rewrite" value="http{R:1}://sub.domain.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
</configuration>
顺便说一句,qBittorrent 会就此问题向您发出警告。记得检查执行日志选项卡。
包含 Web 界面的应用程序是 运行 在安装了 IIS 10 和 ARR 的 Windows Server 2019 系统上。使用 IP:Port 导航到网页时,网页会正确加载。使用 domain.com 导航至该网页时,该网站的某些内容在控制台中显示 401 错误,并且页面无法正确加载。
当导航到域时,请求通过 IIS 和 URL 重写。似乎在通过代理传递信息方面存在一些问题。
这是 IIS 中重写规则的代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:33337/{R:1}" />
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:33337\/?(.*)" />
<action type="Rewrite" value="http{R:1}://sub.domain.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
</configuration>
这是针对此特定问题的 GitHub 问题 link: https://github.com/qbittorrent/qBittorrent/issues/11207
我重现了这个问题。
显然 qBittorrent 期望客户端发送 same-origin Referer
headers。在您的情况下,它必须是 localhost:33337
但显然 sub.domain.com
正在发送。
此安全措施由 Enable Cross-Site Request Forgery (CSRF) protection
设置激活,可通过 qBitorrent > Options > Web UI > Security
访问。
您有两种解决方案。
- 禁用该设置。
- 用适当的值重写
Referer
header。
如果你想重写 header,在允许服务器变量 HTTP_REFERER
和 HTTP_ORIGIN
之后,就像你对 HTTP_ACCEPT_ENCODING
所做的那样,你应该改变你的规则如下。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:33337/{R:1}" />
<!-- New Optional Condition -->
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_REFERER}" pattern="^(?:https?://[^/]*/(.*))?$" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="" />
<!-- New Header Rewrite -->
<set name="HTTP_REFERER" value="http://localhost:33337/{C:1}" />
<!-- Remove Origin Header -->
<set name="HTTP_ORIGIN" value="" />
</serverVariables>
</rule>
</rules>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:33337\/?(.*)" />
<action type="Rewrite" value="http{R:1}://sub.domain.com/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
</configuration>
顺便说一句,qBittorrent 会就此问题向您发出警告。记得检查执行日志选项卡。