URL Rewrite in Azure Front Door (Preview) Standard/Premium
Asked Answered
A

2

7

We have 2 app services: foo.azurewebsites.net and bar.azurewebsites.net and have configured origin groups for both sites. We would like the front door to do path based routing: i.e.:

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. 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. 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:

Setting Value
Action URL rewrite
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/. Is there something we are missing?

Aluin answered 28/7, 2021 at 17:27 Comment(2)
did my answer help you in this case? If not, it would be interesting for me what is not working, so I may can update my answer.Golf
Please accept Pirmin Rehm answer. It is great and explain everythingWalhalla
G
16

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:

If in the Endpoint Manager a Route is configured to match a sub path, e.g. /foo/*, to route to a specific Origin, an enabled UrlRewrite Rule is only applied for the path which is following /foo. e.g.

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

will result in a rewrite from e.g. /foo/baz to /foo/bar/baz and thus will not work for the given issue. 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

The Origin path overwrites the Pattern to match and is thus itself already an 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:

Update 2022 - you can now also set '/' as origin path via Azure Portal

screenshot of azure portal route config

No extra Rules are required here.

Way 2: Don't use different Routes but Rules with Origin group overwrite

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: enter image description here

Golf answered 24/8, 2021 at 21:43 Comment(3)
Man you are my day saver. Great solution. I did try use routes but now I see i do not need them at all. Just use origin path. ThanksWalhalla
I was struggling with terraform and cdn_frontdoor_origin_path = "/" did the trick. You saved my day. ☕Anisole
I wasn't able to get the second method to work, first one works wellCottontail
F
-2

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

Forefinger answered 25/8, 2021 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.