简体   繁体   English

URL 在 Azure Front Door (Preview) Standard/Premium 中重写

[英]URL Rewrite in Azure Front Door (Preview) Standard/Premium

We have 2 app services: foo.azurewebsites.net and bar.azurewebsites.net and have configured origin groups for both sites.我们有 2 个应用程序服务: foo.azurewebsites.netbar.azurewebsites.net并为这两个站点配置了源组。 We would like the front door to do path based routing: ie:我们希望前门进行基于路径的路由:即:

myfd.z01.azurefd.net/foo -> foo.azurewebsites.net
myfd.z01.azurefd.net/bar -> bar.azurewebsites.net

We can configure this to work using Patterns to match on the routes as /foo/* and /bar/* respectively.我们可以将其配置为使用Patterns to match路由,分别为/foo/*/bar/* This works as expected.这按预期工作。

Next, we want to rewrite the URL so that we don't get the initial /foo or /bar path sent to the web apps.接下来,我们要重写 URL,这样我们就不会得到发送到 web 应用程序的初始/foo/bar路径。 In other words, the current setup produces the following:换句话说,当前设置产生以下内容:

myfd.z01.azurefd.net/foo/abc -> foo.azurewebsites.net/foo/abc
myfd.z01.azurefd.net/bar/def -> bar.azurewebsites.net/bar/def

What we want is:我们想要的是:

myfd.z01.azurefd.net/foo/abc -> foo.azurewebsites.net/abc
myfd.z01.azurefd.net/bar/def -> bar.azurewebsites.net/def

So we set up a rewrite URL rule as follows:所以我们设置了一个重写 URL 规则如下:

Setting环境 Value价值
Action行动 URL rewrite URL 重写
Source pattern来源模式 /foo/
Destination目的地 /
Preserve unmatched path保留不匹配的路径 Yes

However, this does not appear to work.但是,这似乎不起作用。 In the AzureDiagnostics logs we can see the rule is firing, but the URL is not being rewritten - it still contains /foo/ .AzureDiagnostics日志中,我们可以看到规则正在触发,但 URL 没有被重写 - 它仍然包含/foo/ Is there something we are missing?我们缺少什么吗?

Problem analysis问题分析

I came across the same issue and after some experiments I derive the following behavior for the current state of the Azure Front Door (Preview) Standard/Premium:我遇到了同样的问题,经过一些实验后,我得出了 Azure Front Door(预览版)标准版/高级版的当前 state 的以下行为:

If in the Endpoint Manager a Route is configured to match a sub path, eg /foo/* , to route to a specific Origin, an enabled UrlRewrite Rule is only applied for the path which is following /foo .如果在端点管理器中路由被配置为匹配子路径,例如/foo/* ,以路由到特定源,则启用的 UrlRewrite 规则仅适用于/foo之后的路径。 eg例如

Route:
  Pattern to match: /foo/*
  OriginGroup: Foo
Rule: UrlRewrite
  Source pattern "/"
  Destination "/bar/"
  Preserve unmatched path: true

will result in a rewrite from eg /foo/baz to /foo/bar/baz and thus will not work for the given issue.将导致从例如/foo/baz重写为/foo/bar/baz ,因此不适用于给定的问题。 However, there are two options to accomplish the desired result.但是,有两种选择可以实现所需的结果。

Solutions解决方案

Way 1: Use the Origin path in the Route config instead of a Rule方式 1:在 Route 配置中使用Origin 路径而不是 Rule

The Origin path overwrites the Pattern to match and is thus itself already an UrlRewrite. Origin 路径会覆盖要匹配的 Pattern,因此它本身已经是一个 UrlRewrite。 Nevertheless, via the Azure Portal UI it is not allowed to set only / as path, but via the az afd route update CLI it is possible and works:尽管如此,通过 Azure 门户 UI 不允许仅设置/作为路径,但通过az afd route update CLI 可以并且有效:

Update 2022 - you can now also set '/' as origin path via Azure Portal 2022 年更新- 您现在还可以通过 Azure 门户将“/”设置为原始路径

Azure 门户路由配置的屏幕截图

No extra Rules are required here.这里不需要额外的规则。

Way 2: Don't use different Routes but Rules with Origin group overwrite方式 2:不要使用不同的路由,而是使用Origin 组覆盖的规则

Only one Route is configured matching all requests /* and serving a default Origin.只有一个路由配置匹配所有请求/*并服务于默认来源。 To this route a Rule set is applied which contains for each origin an Origin group overwerite and a * Url rewrite* rule:此路由应用了一个规则集,其中包含每个来源的来源组覆盖和 * Url 重写* 规则: 在此处输入图像描述

You do not need to use rewrite for this, just use simple forwarding with a route rule to match your pattern and forward to respective back end pools您不需要为此使用重写,只需使用带有路由规则的简单转发来匹配您的模式并转发到相应的后端池

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

相关问题 有没有办法使用 Bicep 将 Azure 防火墙连接到 Front Door Premium Policy? - Is there a way to connect an Azure firewall to a Front Door Premium Policy with Bicep? Azure 前门 Azure 信号 R - Azure Front Door with Azure Signal R Azure 到应用服务子目录的前门路由 - Azure Front Door route to app service subdirectory 如何将 Azure Front Door 与 Azure 容器应用程序一起使用? - How to use Azure Front Door with Azure Container Apps? Azure CDN Premium Verizon 规则引擎 V4 URL 存储重写 Static 网站子目录 - Azure CDN Premium Verizon Rules Engine V4 URL Rewrite for Storage Static Website sub directories 在 Azure Front Door 后端实例之间同步 memory 数据 - Synchronising in memory data between Azure Front Door back end instances 我们可以取消删除或撤消或恢复已删除的 Azure Front Door 实例吗 - Can we undelete or undo or recover the deleted Azure Front Door instance 如何使用 Bicep 为 Azure Front Door 路由激活压缩? - How to activate compression for Azure Front Door routes using Bicep? Azure Front Door Designer - 更新自定义域 - 新 SSL - Azure Front Door Designer - Update Custom Domain - New SSL 使用 terraform 如何将自定义域添加到 Azure 前门 - use terraform how to add custom domain to Azure front door
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM