Azure · Application Gateway · West US VM + App Gateway field guide

Clear the routeto port 80

Put an Ubuntu VM running Nginx behind an Azure Application Gateway: one VNet, two subnets, an NSG, a cloud-init script and a Basic-tier gateway. Nine moves, each with the Portal clicks and the exact az commands.

xfusion-vnet · 10.10.0.0/16Standard_B1sBasic SKUNginx via cloud-initExpect HTTP 200
Route setting panel · xfusion-agwSetting route…
$ curl -sS -o /dev/null -w "%{http_code}" http://$AGW_PUBLIC_IPwaiting
The resources

Eleven resources, → one route

Everything this build creates, with its name, address or setting, and the move that creates it. The names follow the lab's xfusion- prefix; the script builder lets you pick your own.

Departures · West US
--:--
MoveResourceNameDetails
The network

Two subnets. → One way in.

The gateway gets a subnet of its own and is the only public path for web traffic. The VM sits in the other subnet behind the NSG. Hover or tap any part of the map.

xfusion-vnet10.10.0.0/16 · West US xfusion-agw-subnet10.10.2.0/24 · gateway only xfusion-vm-subnet10.10.1.0/24 Internet SSH :22 → VM public IP agw-ip :80 xfusion-agwBasic · listener → rule→ settings :80 → pool NSG80 ✓ · 22 ✓ xfusion-vmUbuntu · B1s · Nginx10.10.1.x (private)
The nine moves

Lay the track → in order

Each move depends on the one before: the gateway needs the VM's private IP, the VM needs the NSG and subnet, and the subnet needs the VNet. Every move shows the Portal route and the CLI.

Move 1 · Prerequisites

Sign in and get an SSH key

Log in to Azure and make sure a key pair exists. The VM uses SSH key authentication, so the Portal wizard needs your public key pasted in as text.

Keep secrets out of scripts. Sign in with az login --use-device-code instead of writing a username and password into the script, even as a comment.

# Log in if not already authenticated
az login --use-device-code
az account show --output table

# Generate an SSH key pair if one doesn't exist
SSH_KEY_PATH="$HOME/.ssh/id_rsa"
if [ ! -f "$SSH_KEY_PATH.pub" ]; then
  ssh-keygen -t rsa -b 4096 -f "$SSH_KEY_PATH" -N ""
fi
cat "$SSH_KEY_PATH.pub"
Move 2 · Network

Create the VNet and two subnets

The Application Gateway needs a subnet of its own, separate from the VM's.

xfusion-vnet 10.10.0.0/16xfusion-vm-subnet 10.10.1.0/24xfusion-agw-subnet 10.10.2.0/24
RESOURCE_GROUP=$(az group list --query "[0].name" -o tsv)
LOCATION="westus"
VNET_NAME="xfusion-vnet";          VNET_PREFIX="10.10.0.0/16"
VM_SUBNET_NAME="xfusion-vm-subnet";  VM_SUBNET_PREFIX="10.10.1.0/24"
AGW_SUBNET_NAME="xfusion-agw-subnet"; AGW_SUBNET_PREFIX="10.10.2.0/24"

az network vnet create \
  --resource-group "$RESOURCE_GROUP" --name "$VNET_NAME" \
  --address-prefix "$VNET_PREFIX" --location "$LOCATION"

az network vnet subnet create \
  --resource-group "$RESOURCE_GROUP" --vnet-name "$VNET_NAME" \
  --name "$VM_SUBNET_NAME" --address-prefix "$VM_SUBNET_PREFIX"

az network vnet subnet create \
  --resource-group "$RESOURCE_GROUP" --vnet-name "$VNET_NAME" \
  --name "$AGW_SUBNET_NAME" --address-prefix "$AGW_SUBNET_PREFIX"
Move 3 · Security

Create the NSG: allow 80 and 22

Two inbound rules: Allow-HTTP on 80 at priority 1000, Allow-SSH on 22 at priority 1010.

Don't skip Allow-SSH. Attaching your own NSG when you create the VM means Azure doesn't add its usual SSH rule. Without this one, you're locked out.

NSG_NAME="xfusion-nsg"

az network nsg create \
  --resource-group "$RESOURCE_GROUP" --name "$NSG_NAME" --location "$LOCATION"

az network nsg rule create \
  --resource-group "$RESOURCE_GROUP" --nsg-name "$NSG_NAME" \
  --name "Allow-HTTP" --priority 1000 --direction Inbound \
  --access Allow --protocol Tcp \
  --source-address-prefixes '*' --source-port-ranges '*' \
  --destination-address-prefixes '*' --destination-port-ranges 80

az network nsg rule create \
  --resource-group "$RESOURCE_GROUP" --nsg-name "$NSG_NAME" \
  --name "Allow-SSH" --priority 1010 --direction Inbound \
  --access Allow --protocol Tcp \
  --source-address-prefixes '*' --source-port-ranges '*' \
  --destination-address-prefixes '*' --destination-port-ranges 22
Move 4 · User data

Write the Nginx cloud-init script

This is the VM's user data. It runs once, on first boot: update packages, install Nginx, enable and start it.

In the Portal there's nothing to create here. Keep the text handy and paste it into Advanced → Custom data in move 5.

CLOUD_INIT_FILE="/tmp/nginx-cloud-init.txt"

cat > "$CLOUD_INIT_FILE" <<'EOF'
#cloud-config
package_update: true
packages:
  - nginx
runcmd:
  - systemctl enable nginx
  - systemctl start nginx
EOF
Move 5 · Compute

Create the virtual machine

Ubuntu 22.04 on Standard_B1s, SSH key login, Standard HDD, in the VM subnet, with your NSG attached and the cloud-init script as custom data.

--storage-sku Standard_LRS = Standard HDD--nsg xfusion-nsg
VM_NAME="xfusion-vm"
VM_SIZE="Standard_B1s"
VM_ADMIN_USER="azureuser"
SSH_PUBLIC_KEY=$(cat "$SSH_KEY_PATH.pub")

az vm create \
  --resource-group "$RESOURCE_GROUP" --name "$VM_NAME" \
  --image "Ubuntu2204" --size "$VM_SIZE" --location "$LOCATION" \
  --admin-username "$VM_ADMIN_USER" \
  --ssh-key-values "$SSH_PUBLIC_KEY" --authentication-type ssh \
  --storage-sku Standard_LRS \
  --vnet-name "$VNET_NAME" --subnet "$VM_SUBNET_NAME" \
  --nsg "$NSG_NAME" \
  --custom-data "$CLOUD_INIT_FILE" \
  --public-ip-sku Standard
Re-running? Clear out a failed VM first
if az vm show -g "$RESOURCE_GROUP" -n "$VM_NAME" >/dev/null 2>&1; then
  az vm delete -g "$RESOURCE_GROUP" -n "$VM_NAME" --yes --no-wait
  while az vm show -g "$RESOURCE_GROUP" -n "$VM_NAME" >/dev/null 2>&1; do
    sleep 10
  done
fi
az disk list -g "$RESOURCE_GROUP" \
  --query "[?starts_with(name, '$VM_NAME')].name" -o tsv | \
  xargs -r -I{} az disk delete -g "$RESOURCE_GROUP" -n {} --yes
Move 6 · Address

Read the VM's private IP

The gateway's backend pool targets this address directly. Traffic from the gateway to the VM stays inside the VNet.

VM_PRIVATE_IP=$(az vm show \
  --resource-group "$RESOURCE_GROUP" --name "$VM_NAME" \
  --show-details --query privateIps --output tsv)

echo "VM private IP: $VM_PRIVATE_IP"
Move 7 · Front door

Create the gateway's public IP

Standard SKU, static. The gateway needs a Standard public IP. In the Portal you can also create it inline in move 8.

AGW_PIP_NAME="xfusion-agw-ip"

az network public-ip create \
  --resource-group "$RESOURCE_GROUP" --name "$AGW_PIP_NAME" \
  --sku Standard --allocation-method Static \
  --location "$LOCATION"
Move 8 · The gateway

Create the Application Gateway (Basic)

Listener on HTTP :80 → routing rule → HTTP settings on :80 → backend pool with the VM's private IP.

Why az rest? The CLI's --sku check doesn't accept the Basic tier yet. The script sends the ARM request body straight to the REST API instead, which skips that client-side check.

Check this body against your script. The request body in the source guide is cut off, so the one shown here is rebuilt from the Application Gateway ARM schema (API 2023-11-01).

AGW_NAME="xfusion-agw"
# Remove a failed earlier deployment, if any
if az network application-gateway show -g "$RESOURCE_GROUP" -n "$AGW_NAME" >/dev/null 2>&1; then
  az network application-gateway delete -g "$RESOURCE_GROUP" -n "$AGW_NAME"
fi

AGW_BACKEND_POOL="xfusion-backendpool"
AGW_HTTP_SETTINGS="xfusion-http-settings"
AGW_LISTENER="xfusion-listener"
AGW_ROUTING_RULE="xfusion-routing-rule"

VNET_ID=$(az network vnet show -g "$RESOURCE_GROUP" -n "$VNET_NAME" --query id -o tsv)
AGW_SUBNET_ID="$VNET_ID/subnets/$AGW_SUBNET_NAME"
AGW_PIP_ID=$(az network public-ip show -g "$RESOURCE_GROUP" -n "$AGW_PIP_NAME" --query id -o tsv)
SUBSCRIPTION_ID=$(az account show --query id -o tsv)
AGW_RESOURCE_ID="/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.Network/applicationGateways/$AGW_NAME"
API_VERSION="2023-11-01"

# Request body (rebuilt; see note)
AGW_BODY_FILE="/tmp/agw-body.json"
cat > "$AGW_BODY_FILE" <<EOF
{
  "location": "$LOCATION",
  "properties": {
    "sku": { "name": "Basic", "tier": "Basic" },
    "gatewayIPConfigurations": [{ "name": "agw-ipcfg",
      "properties": { "subnet": { "id": "$AGW_SUBNET_ID" } } }],
    "frontendIPConfigurations": [{ "name": "agw-frontend",
      "properties": { "publicIPAddress": { "id": "$AGW_PIP_ID" } } }],
    "frontendPorts": [{ "name": "port-80", "properties": { "port": 80 } }],
    "backendAddressPools": [{ "name": "$AGW_BACKEND_POOL",
      "properties": { "backendAddresses": [{ "ipAddress": "$VM_PRIVATE_IP" }] } }],
    "backendHttpSettingsCollection": [{ "name": "$AGW_HTTP_SETTINGS",
      "properties": { "port": 80, "protocol": "Http",
        "cookieBasedAffinity": "Disabled", "requestTimeout": 30 } }],
    "httpListeners": [{ "name": "$AGW_LISTENER", "properties": {
      "frontendIPConfiguration": { "id": "$AGW_RESOURCE_ID/frontendIPConfigurations/agw-frontend" },
      "frontendPort": { "id": "$AGW_RESOURCE_ID/frontendPorts/port-80" },
      "protocol": "Http" } }],
    "requestRoutingRules": [{ "name": "$AGW_ROUTING_RULE", "properties": {
      "ruleType": "Basic", "priority": 100,
      "httpListener": { "id": "$AGW_RESOURCE_ID/httpListeners/$AGW_LISTENER" },
      "backendAddressPool": { "id": "$AGW_RESOURCE_ID/backendAddressPools/$AGW_BACKEND_POOL" },
      "backendHttpSettings": { "id": "$AGW_RESOURCE_ID/backendHttpSettingsCollection/$AGW_HTTP_SETTINGS" } } }]
  }
}
EOF

az rest --method put \
  --url "https://management.azure.com${AGW_RESOURCE_ID}?api-version=${API_VERSION}" \
  --body @"$AGW_BODY_FILE"
Move 9 · Verify

Wait for it, then test the route

Gateway deployment takes several minutes. Wait for Provisioning state: Succeeded, then give cloud-init about a minute to finish installing Nginx.

Expect HTTP status: 200. If the first try fails, the gateway or Nginx may still be starting. Wait and retry.

# Block until provisioning finishes
az network application-gateway wait \
  --resource-group "$RESOURCE_GROUP" --name "$AGW_NAME" --created

AGW_PUBLIC_IP=$(az network public-ip show \
  --resource-group "$RESOURCE_GROUP" --name "$AGW_PIP_NAME" \
  --query ipAddress --output tsv)

# Let cloud-init finish installing Nginx
sleep 60

curl -sS -m 10 -o /dev/null \
  -w "HTTP status: %{http_code}\n" "http://$AGW_PUBLIC_IP"
The first request

200 or 502? → Try it.

A gateway that's up doesn't mean the site is. If Nginx isn't answering yet, the gateway's health probe marks the VM unhealthy and returns 502 Bad Gateway. This is a simulation you can run.

Set the VM's state, then send the request.

cloud-init has finished: Nginx is enabled and listening on port 80.

$ curl -sS -m 10 -o /dev/null -w "HTTP status: %{http_code}\n" "http://$AGW_PUBLIC_IP" Press "Send request" to run the simulation.
Your script

Name the route. → Get the script.

Pick a prefix, region and address space. You get one bash script covering all nine moves, with every resource named consistently.

2–20 lowercase letters, numbers or hyphens. Becomes prefix-vnet, prefix-agw and so on.
A /16. The VM and gateway subnets are the .1.0/24 and .2.0/24 inside it.
setup-agw-vm.shbash
Route set
Sign-off

Eight signals → to green

Tap each signal as you confirm it in Azure. All eight green means traffic runs end to end.

0 of 8 signals clear