【问题标题】:Redirect of some API requests from one Web App to another将一些 API 请求从一个 Web 应用重定向到另一个
【发布时间】:2020-05-15 18:10:37
【问题描述】:
我有两个 Azure Web 应用程序。我想配置一些 API 请求从一个 Web 应用程序到另一个的重定向。所有到 /api/v1/resources 的请求都应该转到 webapp2,所有其他到 webapp1 上的示例 /api 的请求都将保留在 web 应用程序 webapp1 上。
<rewrite>
<rules>
<rule name="[RULE NAME]" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="https://webapp1.test.net/api/v1/resources" />
</conditions>
<action type="Redirect" url="https://webapp2.test.net/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
但是我的重定向不起作用。
【问题讨论】:
标签:
azure
iis
azure-web-app-service
【解决方案1】:
我找到了答案:
在 web.config 我添加了
<rewrite>
<rules>
<rule name="Rewrite">
<match url="^api/v1/resources/?(.*)" />
<action type="Rewrite" url="https://webapp2.test.net/api/v1/resources/{R:1}" appendQueryString="true" logRewrittenUrl="false" />
</rule>
</rules>
</rewrite>
并添加文件 applicationHost.xdt
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<proxy xdt:Transform="InsertIfMissing" enabled="true" preserveHostHeader="false" reverseRewriteHostInResponseHeaders="false" />
</system.webServer>
</configuration>
This file will override applicationhost.config, taking advantage of a feature in Azure, called Azure Site Extensions, that provides a mechanism to apply transforms to applicationhost.config using XDT transforms.
【解决方案2】:
你的webconfig中带有<rewrite>标签的设置对IIS和VS调试有效。
您应该使用<RewriterConfig>标签的代码,以便将其部署到Azure Web应用程序中才能生效。以下是示例代码,条件和操作需要自己设置。更多详情,你可以参考我在另一个post的答案。
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler,URLRewriter" />
</configSections>
......
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>^default/([0-9]+)/([_0-9a-z-]+)</LookFor>
<SendTo>11.aspx?id={R:1}</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
【解决方案3】:
只是因为 {HTTP_HOST}{REQUEST_URI} 不包含 https://。
所以请尝试使用 443 端口的 {CACHE_URL}
<rule name="rulename" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{CACHE_URL}" pattern="https://webapp1.test.net:443/api/v1/resources" />
</conditions>
<action type="Redirect" url="https://webapp2.test.net/{R:0}" redirectType="Permanent" />
</rule>
或{https}=^on$
的{MATCH All}
<rule name="123" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}{REQUEST_URI}" pattern="webapp1.test.net/api/v1/resources" />
<add input="{HTTPS}" pattern="^on$" />
</conditions>
<action type="Redirect" url="https://webapp2.test.net/{R:0}" redirectType="Permanent" />
</rule>