简体   繁体   English

IIS 特定 URL 路径的反向代理 - URL 重写模块错误

[英]IIS Reverse Proxy for specific URL path - URL Rewrite Module Error

I have one windows service which is running some application with REST endpoint on localhost:5001/api/... So I installed IIS 10 with rewrite module to create reverse proxy for HTTPS to that service. I have one windows service which is running some application with REST endpoint on localhost:5001/api/... So I installed IIS 10 with rewrite module to create reverse proxy for HTTPS to that service. That worked great.那效果很好。

Now I want to use the same IIS site for hosting a website, which should consume that service on same url.现在我想使用同一个 IIS 站点来托管一个网站,它应该在同一个 url 上使用该服务。 So I tried to change the pattern of my iis rewrite url to 'api/(.*)' so it should just proxy the /api requests.所以我试图改变我的 iis 重写 url 到 'api/(.*)' 的模式,所以它应该只是代理 /api 请求。 Testing the pattern is exactly what I want to: Browsing server.domain/api/function matches and the rewrite is done and server.domain is not rewritten.测试模式正是我想要的:浏览 server.domain/api/function 匹配并且重写完成并且 server.domain 没有被重写。 But now browsing server.domain will often return 'HTTP Error 500.52 - URL Rewrite Module Error'.但是现在浏览 server.domain 经常会返回“HTTP Error 500.52 - URL Rewrite Module Error”。 Sometimes loading the page works great.有时加载页面效果很好。 But after reload I will get that error again.但是重新加载后我会再次收到该错误。

My web.config:我的 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>

I have no idea why there are two outboundRules.我不知道为什么有两个 outboundRules。 IIS created them automatically when I decided to create reverse proxy rules.当我决定创建反向代理规则时,IIS 自动创建了它们。

If I disable static content compression, all will work without problems.如果我禁用 static 内容压缩,一切都会正常工作。 Dynamic compression module is not installed.未安装动态压缩模块。

So I have multiple questions: - why do I get the "Rewrite URL" error for pages which should not be rewritten and the rewritten ones will work without any problem - why static compression creates rewrite url errors - how should I configure my IIS to do compression and work with rewriting (I checked the module order and the static compression module is above the rewrite module).所以我有多个问题: - 为什么我会收到不应重写的页面的“重写 URL”错误,而重写的页面将毫无问题地工作 - 为什么 static 压缩会创建重写 url 错误 - 我应该如何配置我的 Z51259ACF4601BECB28压缩和重写工作(我检查了模块顺序,static 压缩模块在重写模块之上)。

Best, Robin最好的,罗宾

Did you create reverse proxy rule by using URL rewrite rule template?您是否使用 URL 重写规则模板创建了反向代理规则? If so, you might enable outbound rules.如果是这样,您可能会启用出站规则。 These outbound rules are mainly used when a client can't access source file behind DMZ server.这些出站规则主要在客户端无法访问 DMZ 服务器后面的源文件时使用。 If you client are able to access the domain on backend server, then you don't need these outbound rule.如果您的客户端能够访问后端服务器上的域,那么您不需要这些出站规则。

The reason for IIS return 500.52 error is IIS can't apply outbound rule for a encoded response body from backend server. IIS 返回 500.52 错误的原因是 IIS 无法对来自后端服务器的编码响应正文应用出站规则。

So you have to either disable httpcompression or rewrite the incoming accept-encoding header.因此,您必须禁用 httpcompression 或重写传入的接受编码 header。

I'm afraid it is unavailable to make httpcompression and outbound rule work side by side because rewrite module can't rewrite a compressed entity.恐怕无法使 httpcompression 和出站规则同时工作,因为重写模块无法重写压缩实体。

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 https://docs.microsoft.com/zh-cn/archive/blogs/friis/iis-with-url-rewrite-as-a-reverse-proxy-part-2-dealing-with-500-52-status-代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM