【问题标题】:IIS Reverse Proxy for specific URL path - URL Rewrite Module Error特定 URL 路径的 IIS 反向代理 - URL 重写模块错误
【发布时间】:2020-07-29 16:28:27
【问题描述】:

我有一个 Windows 服务,它在 localhost:5001/api/... 上运行一些带有 REST 端点的应用程序 因此,我安装了带有重写模块的 IIS 10,为该服务的 HTTPS 创建反向代理。效果很好。

现在我想使用同一个 IIS 站点来托管一个网站,它应该在同一个 url 上使用该服务。 所以我尝试将我的 iis 重写 url 的模式更改为 'api/(.*)' 所以它应该只是代理 /api 请求。测试模式正是我想要的:浏览 server.domain/api/function 匹配并且重写完成并且 server.domain 没有被重写。 但是现在浏览 server.domain 经常会返回“HTTP Error 500.52 - URL Rewrite Module Error”。 有时加载页面效果很好。但是重新加载后我会再次收到该错误。

我的 web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <outboundRules>
                <rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1" enabled="true">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:5001/(.*)" />
                    <action type="Rewrite" value="https://server.domain/{R:2}" />
                </rule>
                <rule name="ReverseProxyOutboundRule2" preCondition="ResponseIsHtml1" enabled="true">
                    <match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:5001/(.*)" />
                    <action type="Rewrite" value="https://server.domain/{R:2}" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
            <rules>
                <rule name="ReverseProxyInboundRule1" enabled="true" stopProcessing="true">
                    <match url="api/(.*)" />
                    <action type="Rewrite" url="http://localhost:5001/{R:0}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        <httpErrors errorMode="Detailed" />
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Methods" value="POST, GET, OPTIONS, DELETE, PUT" />
                <add name="Access-Control-Max-Age" value="1000" />
                <add name="Access-Control-Allow-Headers" value="x-requested-with, Content-Type, origin, authorization, accept, client-security-token" />
            </customHeaders>
        </httpProtocol>
        <urlCompression doDynamicCompression="false" />
    </system.webServer>
</configuration>

我不知道为什么有两个 outboundRules。当我决定创建反向代理规则时,IIS 自动创建了它们。

如果我禁用静态内容压缩,一切都会正常工作。 未安装动态压缩模块。

所以我有多个问题: - 为什么不应该重写的页面出现“重写 URL”错误,而重写的页面可以正常工作 - 为什么静态压缩会产生重写 url 错误 - 我应该如何配置我的 IIS 来进行压缩和重写(我检查了模块顺序,静态压缩模块在重写模块之上)。

最好, 罗宾

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    您是否使用 URL 重写规则模板创建了反向代理规则?如果是这样,您可能会启用出站规则。这些出站规则主要在客户端无法访问 DMZ 服务器后面的源文件时使用。如果您的客户端能够访问后端服务器上的域,那么您不需要这些出站规则。

    IIS 返回 500.52 错误的原因是 IIS 无法为来自后端服务器的编码响应正文应用出站规则。

    因此您必须禁用 httpcompression 或重写传入的接受编码标头。

    恐怕无法让 httpcompression 和 outbound 规则并行工作,因为 rewrite 模块无法重写压缩实体。

    https://docs.microsoft.com/zh-cn/archive/blogs/friis/iis-with-url-rewrite-as-a-reverse-proxy-part-2-dealing-with-500-52-status-codes

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 2020-05-23
      • 2018-05-04
      • 2012-09-14
      • 2011-01-16
      • 2014-03-24
      • 2013-09-08
      • 2014-02-01
      相关资源
      最近更新 更多