title | description | services | author | ms.service | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|
Tutorial: Manually configure Azure Front Door for Azure Static Web Apps |
Learn set up Azure Front Door for Azure Static Web Apps |
static-web-apps |
craigshoemaker |
static-web-apps |
how-to |
01/12/2022 |
cshoe |
Learn to add Azure Front Door as the CDN for your static web app. Azure Front Door is a scalable and secure entry point for fast delivery of your web applications.
In this tutorial, you learn how to:
[!div class="checklist"]
- Create an Azure Front Door Standard/Premium instance
- Associate Azure Front Door with your Azure Static Web Apps site
Note
This tutorial requires the Azure Static Web Apps Standard and Azure Front Door Standard / Premium plans.
-
Navigate to the Azure portal.
-
Open the static web app that you want to apply Azure Front Door.
-
Go to the Overview section.
-
Copy the URL to your clipboard for later use.
-
Navigate to the Azure home screen.
-
Select Create a resource.
-
Search for Front Door.
-
Select Front Door Standard/Premium.
Make sure to select the service labeled Front Door Standard/Premium and not the plain Front Door option.
-
Select Create.
-
Select the Azure Front Door Standard/Premium option.
-
Select the Quick create option.
-
Select the Continue to create a front door button.
-
In the Basics tab, enter the following values:
Setting Value Subscription Select your Azure subscription. Resource group Enter a resource group name. This name is often the same group name used by your static web app. Resource group location If you create a new resource group, enter the location nearest you. Name Enter my-static-web-app-front-door. Tier Select Standard. Endpoint name Enter a unique name for your Front Door host. Origin type Select Custom. Origin host name Enter the hostname of your static web app that you set aside from the beginning of this tutorial. Make sure your value does not include a trailing slash or protocol. (For example, desert-rain-04056.azurestaticapps.net
)Caching Check the Enable caching checkbox. Query string caching behavior Select Use Query string from the dropdown. -
Select Review + create.
-
Select Create.
The creation process may take a few minutes to complete.
-
Select Go to resource.
Note
The cache expiration, cache key query string and origin group override actions are deprecated. These deprecated actions can still work normally, but your rule set >cannot be changed. You need to replace them with new route configuration override action before changing your rule set.
Add the following settings to disable Front Door's caching policies from trying to cache authentication and authorization-related pages.
-
Under Settings, select Rule set.
-
Select Add.
-
In the Rule set name textbox, enter Security.
-
In the Rule name textbox, enter NoCacheAuthRequests.
-
Select Add a condition.
-
Select Request path.
-
Select Begins With in the Operator drop down.
-
Select the Edit link above the Value textbox.
-
Enter /.auth in the textbox.
-
Select the Update button.
-
Select the No transform option from the Case transform dropdown.
-
Select the Add an action dropdown.
-
Select Route configuration override.
-
Select Disabled in the Caching dropdown.
-
Select the Save button.
Now that the rule is created, you apply the rule to a Front Door endpoint.
-
Select the Unassociated link.
-
Select the Endpoint name to which you want to apply the caching rule.
-
Select the Next button.
-
Select the Associate button.
Use the following steps to copy the Front Door instance's unique identifier.
-
Select the Overview link on the left-hand navigation.
-
From the Overview window, copy the value labeled Front Door ID and paste it into a file for later use.
To complete the integration with Front Door, you need to update the application configuration file to:
- Restrict traffic to your site only through Front Door
- Restrict traffic to your site only from your Front Door instance
- Define which domains can access your site
- Disable caching for secured routes
Open the staticwebapp.config.json file for your site and make the following changes.
-
Restrict traffic to only use Front Door by adding the following section to the configuration file:
"networking": { "allowedIpRanges": ["AzureFrontDoor.Backend"] }
-
To define which Azure Front Door instances and domains can access your site, add the
forwardingGateway
section."forwardingGateway": { "requiredHeaders": { "X-Azure-FDID" : "<YOUR-FRONT-DOOR-ID>" }, "allowedForwardedHosts": [ "my-sitename.azurefd.net" ] }
First, configure your app to only allow traffic from your Front Door instance. In every backend request, Front Door automatically adds an
X-Azure-FDID
header that contains your Front Door instance ID. By configuring your static web app to require this header, it will restrict traffic exclusively to your Front Door instance. In theforwardingGateway
section in your configuration file, add therequiredHeaders
section and define theX-Azure-FDID
header. Replace<YOUR-FRONT-DOOR-ID>
with the Front Door ID you set aside earlier.Next, add the Azure Front Door hostname (not the Azure Static Web Apps hostname) into the
allowedForwardedHosts
array. If you have custom domains configured in your Front Door instance, also include them in this list.In this example, replace
my-sitename.azurefd.net
with the Azure Front Door hostname for your site. -
For all secured routes in your app, disable Azure Front Door caching by adding
"Cache-Control": "no-store"
to the route header definition.{ "route": "/members", "allowedRoles": ["authenticated, members"], "headers": { "Cache-Control": "no-store" } }
With this configuration, your site is no longer available via the generated *.azurestaticapps.net
hostname, but exclusively through the hostnames configured in your Front Door instance.
-
Custom domains: Now that Front Door is managing your site, you no long use the Azure Static Web Apps custom domain feature. Azure Front Door has a separate process for adding a custom domain. Refer to Add a custom domain to your Front Door. When you add a custom domain to Front Door, you'll need to update your static web app configuration file to include it in the
allowedForwardedHosts
list. -
Traffic statistics: By default, Azure Front Door configures health probes that may affect your traffic statistics. You may want to edit the default values for the health probes.
-
Serving old versions: When you deploy updates to existing files in your static web app, Azure Front Door may continue to serve older versions of your files until their time-to-live expires. Purge the Azure Front Door cache for the affected paths to ensure the latest files are served.
If you no longer want to use the resources created in this tutorial, delete the Azure Static Web Apps and Azure Front Door instances.
[!div class="nextstepaction"] Add an API