# IP restricting Azure Application Gateway

Published: 7 May 2021
Category: Technology

I couldn't find an article on how to do this easily, so this is more for self-reference.

I also don't like screenshots with arrows all over them, so this will use the `azure-cli` commands for macOS/Linux.  It should be easy enough to figure out how to translate them to Powershell commands or Azure Portal clicks.

---

It turns out that applying an IP whitelist to Application Gateway isn't actually done on the gateway.

These instructions assume that you already have an Application Gateway configured.

Instead, the process is to create (or configure) a [Network Security Group](https://docs.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview) in the subnet of the [Virtual Network](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-networks-overview).

Start by creating a Network Security Group

~~~sh
az network nsg create \
  --name example-nsg \
  --resource-group example-rg
~~~

Add a rule to the group allowing access from the Application Gateway, you won't be able to assign this to the subnet of the gateway if you don't have this rule.

~~~sh
az network nsg rule create \
  --resource-group example-rg \
  --nsg-name example-nsg \
  --name example-nsg-allow-ag-nsgsr \
  --priority 100 \
  --access Allow \
  --destination-port-ranges 65200-65535
~~~

Now define the rules to deny access for HTTP/HTTPS.

~~~sh
az network nsg rule create \
  --resource-group example-rg \
  --nsg-name example-nsg \
  --name example-nsg-deny-web-nsgsr \
  --priority 500 \
  --access Deny \
  --destination-port-ranges 80 443
~~~

Finally, create the rules to allow access from your whitelisted IP ranges.

~~~sh
az network nsg rule create \
  --resource-group example-rg \
  --nsg-name example-nsg \
  --name example-nsg-allow-whitelist-web-nsgsr \
  --priority 200 \
  --access Allow \
  --source-address-prefixes 208.130.28/24 \
  --destination-port-ranges 80 443
~~~

**n.b.** The priority of the Allow rule must be *lower* than the Deny rule.

Now you can associate the Network Security Group with the Virtual Network's subnet.

~~~sh
az network vnet subnet update \
  --resource-group example-rg \
  --name example-snet \
  --vnet-name example-vnet \
  --network-security-group example-nsg
~~~

Once that propagates all traffic directed to your subnet will be subject to the rules you have defined.

This example blocks all traffic on ports 80 and 443 for that subnet, if you have other web services in the subnet that you don't want to be subjected to the whitelisting you can specify the destinations for the rules.

**References**

- [az network nsg create](https://docs.microsoft.com/en-us/cli/azure/network/nsg#az_network_nsg_create)
- [az network nsg rule create](https://docs.microsoft.com/en-us/cli/azure/network/nsg/rule#az_network_nsg_rule_create)
- [az network vnet subnet update](https://docs.microsoft.com/en-us/cli/azure/network/vnet/subnet#az_network_vnet_subnet_update)
