Skip to content

Commit 7068af0

Browse files
authored
Merge pull request #299365 from halkazwini/waf-custom
Updates: Configure a custom response for Azure Web Application Firewall
2 parents eb392af + 70e2424 commit 7068af0

File tree

3 files changed

+61
-71
lines changed

3 files changed

+61
-71
lines changed

articles/web-application-firewall/afds/waf-front-door-configure-custom-response-code.md

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,112 @@
11
---
2-
title: Configure custom responses for Web Application Firewall with Azure Front Door
2+
title: Configure custom responses for Azure Front Door WAF policy
33
description: Learn how to configure a custom response code and message when Azure Web Application Firewall blocks a request.
44
author: halkazwini
55
ms.author: halkazwini
66
ms.service: azure-web-application-firewall
77
ms.topic: how-to
8-
ms.date: 04/04/2025
8+
ms.date: 05/08/2025
99
ms.custom: devx-track-azurepowershell
10-
zone_pivot_groups: front-door-tiers
1110
---
1211

1312
# Configure a custom response for Azure Web Application Firewall
1413

15-
In this article, you learn how to configure a custom response page when Azure Web Application Firewall blocks a request.
14+
By default, when Azure Web Application Firewall (WAF) blocks a request because of a matched rule, it returns a 403 status code with the message "The request is blocked." The default message also includes the tracking reference string that's used to link to [log entries](./waf-front-door-monitor.md) for the request.
1615

17-
By default, when Azure Web Application Firewall blocks a request because of a matched rule, it returns a 403 status code with the message "The request is blocked." The default message also includes the tracking reference string that's used to link to [log entries](./waf-front-door-monitor.md) for the request. You can configure a custom response status code and a custom message with a reference string for your use case.
16+
In this article, you learn how to configure a custom response status code and a custom message with a reference string for your use case using the Azure portal, PowerShell, or the Azure CLI.
1817

19-
## Configure a custom response status code and message
18+
## Prerequisites
2019

2120
# [**Portal**](#tab/portal)
2221

23-
You can configure a custom response status code and body under **Policy settings** on the Azure Web Application Firewall portal.
22+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
2423

25-
:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png" alt-text="Screenshot that shows Azure Web Application Firewall Policy settings.":::
24+
- Sign in to the [Azure portal](https://portal.azure.com) with your Azure account.
2625

27-
In the preceding example, we kept the response code as 403 and configured a short "Please contact us" message, as shown in the following image:
26+
# [**PowerShell**](#tab/powershell)
2827

29-
:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response.png" alt-text="Screenshot that shows a custom response example.":::
28+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
3029

31-
# [**PowerShell**](#tab/powershell)
30+
- Azure Cloud Shell or Azure PowerShell.
3231

33-
Follow these steps to configure a custom response status code and message by using PowerShell.
32+
The steps in this article run the Azure PowerShell cmdlets interactively in [Azure Cloud Shell](/azure/cloud-shell/overview). To run the cmdlets in the Cloud Shell, select **Open Cloud Shell** at the upper-right corner of a code block. Select **Copy** to copy the code and then paste it into Cloud Shell to run it. You can also run the Cloud Shell from within the Azure portal.
3433

35-
### Set up your PowerShell environment
34+
You can also [install Azure PowerShell locally](/powershell/azure/install-azure-powershell) to run the cmdlets. This article requires the Azure PowerShell module. If you run PowerShell locally, sign in to Azure using the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet.
3635

37-
Azure PowerShell provides a set of cmdlets that use the [Azure Resource Manager](../../azure-resource-manager/management/overview.md) model for managing your Azure resources.
36+
# [**Azure CLI**](#tab/cli)
3837

39-
You can install [Azure PowerShell](/powershell/azure/) on your local machine and use it in any PowerShell session. Follow the instructions on the page to sign in with your Azure credentials. Then install the Az PowerShell module.
38+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
4039

41-
### Connect to Azure with an interactive dialog for sign-in
40+
- Azure Cloud Shell or Azure CLI.
4241

43-
```
44-
Connect-AzAccount
45-
Install-Module -Name Az
42+
The steps in this article run the Azure CLI commands interactively in [Azure Cloud Shell](/azure/cloud-shell/overview). To run the commands in the Cloud Shell, select **Open Cloud Shell** at the upper-right corner of a code block. Select **Copy** to copy the code, and paste it into Cloud Shell to run it. You can also run the Cloud Shell from within the Azure portal.
4643

47-
```
48-
Make sure you have the current version of PowerShellGet installed. Run the following command and reopen PowerShell.
44+
You can also [install Azure CLI locally](/cli/azure/install-azure-cli) to run the commands. This article requires the Azure CLI version 2.67.0 or higher and **front-door** extension. Run [az --version](/cli/azure/reference-index#az-version) command to find the installed version. If you run Azure CLI locally, sign in to Azure using the [az login](/cli/azure/reference-index#az-login) command.
4945

50-
```
51-
Install-Module PowerShellGet -Force -AllowClobber
52-
```
46+
---
5347

54-
### Install the Az.FrontDoor module
48+
## Configure a custom response status code and message
5549

56-
```
57-
Install-Module -Name Az.FrontDoor
58-
```
50+
# [**Portal**](#tab/portal)
5951

60-
### Create a resource group
52+
To customize the response status code and body, follow these steps:
6153

62-
In Azure, you allocate related resources to a resource group. Here, we create a resource group by using [New-AzResourceGroup](/powershell/module/Az.resources/new-Azresourcegroup).
54+
1. Go to your Front Door WAF policy in the Azure portal.
6355

64-
```azurepowershell-interactive
65-
New-AzResourceGroup -Name myResourceGroupWAF
66-
```
56+
1. Under **Settings**, select **Policy settings**.
6757

68-
### Create a new WAF policy with a custom response
58+
1. Enter the custom response status code and response body in the **Block response status code** and **Block response body** boxes, respectively.
6959

70-
The following example shows how to create a new web application firewall (WAF) policy with a custom response status code set to 405 and a message of "You are blocked" by using
71-
[New-AzFrontDoorWafPolicy](/powershell/module/az.frontdoor/new-azfrontdoorwafpolicy).
60+
:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png" alt-text="Screenshot that shows Azure Web Application Firewall Policy settings." lightbox="../media/waf-front-door-configure-custom-response-code/custom-response-settings.png":::
7261

73-
```azurepowershell
74-
# WAF policy setting
75-
New-AzFrontDoorWafPolicy `
76-
-Name myWAFPolicy `
77-
-ResourceGroupName myResourceGroupWAF `
78-
-EnabledState enabled `
79-
-Mode Detection `
80-
-CustomBlockResponseStatusCode 405 `
81-
-CustomBlockResponseBody "<html><head><title>You are blocked.</title></head><body></body></html>"
82-
```
62+
1. Select **Save**.
63+
64+
# [**PowerShell**](#tab/powershell)
8365

84-
Modify the custom response code or response body settings of an existing WAF policy by using [Update-AzFrontDoorFireWallPolicy](/powershell/module/az.frontdoor/Update-AzFrontDoorWafPolicy).
66+
To customize the response status code and body, use [Update-AzFrontDoorWafPolicy](/powershell/module/az.frontdoor/Update-AzFrontDoorWafPolicy) cmdlet.
8567

86-
```azurepowershell
87-
# modify WAF response code
88-
Update-AzFrontDoorFireWallPolicy `
89-
-Name myWAFPolicy `
90-
-ResourceGroupName myResourceGroupWAF `
91-
-EnabledState enabled `
92-
-Mode Detection `
93-
-CustomBlockResponseStatusCode 403
94-
```
9568

96-
```azurepowershell
97-
# modify WAF response body
98-
Update-AzFrontDoorFireWallPolicy `
99-
-Name myWAFPolicy `
100-
-ResourceGroupName myResourceGroupWAF `
101-
-CustomBlockResponseBody "<html><head><title>Forbidden</title></head><body>{{azure-ref}}</body></html>"
69+
```azurepowershell-interactive
70+
# Update WAF policy settings to customize response body and status code
71+
Update-AzFrontDoorWafPolicy `
72+
-Name 'myWAFPolicy' `
73+
-ResourceGroupName 'myResourceGroup' `
74+
-RequestBodyCheck 'Enabled' `
75+
-RedirectUrl 'https://learn.microsoft.com/en-us/azure/web-application-firewall/' `
76+
-CustomBlockResponseStatusCode '403' `
77+
-CustomBlockResponseBody '<html><head><title>WAF Demo</title></head><body><p><h1><strong>WAF Custom Response Page</strong></h1></p><p>Please contact us with this information:<br>{{azure-ref}}</p></body></html>'
10278
```
10379

104-
---
80+
# [**Azure CLI**](#tab/cli)
10581

106-
> [!NOTE]
107-
> If you leave the block response body blank, the WAF returns a *403 Forbidden* response for normal WAF blocks and a *429 Too many requests* for rate limit blocks.
82+
To customize the response status code and body, use [az network front-door waf-policy update](/cli/azure/network/front-door/waf-policy#az-network-front-door-waf-policy-update) command.
10883

109-
::: zone pivot="front-door-standard-premium"
11084

111-
"{{azure-ref}}" inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontDoorAccessLog` and `FrontDoorWebApplicationFirewallLog` logs.
85+
```azurecli-interactive
86+
# Update WAF policy settings to customize response body and status code
87+
az network front-door waf-policy update \
88+
--name 'myWAFPolicy' \
89+
--resource-group 'myResourceGroup' \
90+
--request-body-check 'Enabled' \
91+
--redirect-url 'https://learn.microsoft.com/en-us/azure/web-application-firewall/' \
92+
--custom-block-response-status-code '403' \
93+
--custom-block-response-body 'PGh0bWw+PGhlYWQ+PHRpdGxlPldBRiBEZW1vPC90aXRsZT48L2hlYWQ+PGJvZHk+PHA+PGgxPjxzdHJvbmc+V0FGIEN1c3RvbSBSZXNwb25zZSBQYWdlPC9zdHJvbmc+PC9oMT48L3A+PHA+UGxlYXNlIGNvbnRhY3QgdXMgd2l0aCB0aGlzIGluZm9ybWF0aW9uOjxicj57e2F6dXJlLXJlZn19PC9wPjwvYm9keT48L2h0bWw+'
94+
```
95+
96+
> [!NOTE]
97+
> The value of the `--custom-block-response-body` parameter must be a **base64** encoded string.
11298
113-
::: zone-end
99+
---
114100

115-
::: zone pivot="front-door-classic"
101+
In the previous example, the response code was kept as 403 with a custom message: "*Please contact us with this information:*".
116102

117-
"{{azure-ref}}" inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontdoorAccessLog` and `FrontdoorWebApplicationFirewallLog` logs.
103+
:::image type="content" source="../media/waf-front-door-configure-custom-response-code/custom-response.png" alt-text="Screenshot that shows a custom response example.":::
104+
105+
> [!NOTE]
106+
> `{{azure-ref}}` inserts the unique reference string in the response body. The value matches the TrackingReference field in the `FrontDoorAccessLog` and `FrontDoorWebApplicationFirewallLog` logs.
118107
119-
::: zone-end
108+
> [!IMPORTANT]
109+
> If you leave the block response body blank, the WAF returns a ***403 Forbidden*** response for normal WAF blocks and a ***429 Too many requests*** for rate limit blocks.
120110
121111
## Next step
122112

-18.9 KB
Loading
-8.06 KB
Loading

0 commit comments

Comments
 (0)