此腳本會建立一個應用程式閘道,該閘道具有 Web 應用程式防火牆,並使用虛擬機擴展集作為後端伺服器。 Web 應用程式防火牆會根據 OWASP 規則來限制 Web 流量。 執行指令碼之後,您可以使用應用程式閘道的公用 IP 位址來進行測試。
若要執行此範例,請安裝最新版的 Azure CLI。 若要啟動,請執行 az login
來建立與 Azure 的連線。
Azure CLI 的範例是針對 bash
命令列介面撰寫的。 若要在 Windows PowerShell 或命令提示字元中執行此範例,您可能需要變更指令碼的元素。
如果您沒有 Azure 訂用帳戶,請在開始之前建立 Azure 免費帳戶 。
範例腳本
# Create a resource group
az group create --name myResourceGroupAG --location eastus
# Create network resources
az network vnet create \
--name myVNet \
--resource-group myResourceGroupAG \
--location eastus \
--address-prefix 10.0.0.0/16 \
--subnet-name myAGSubnet \
--subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--name myBackendSubnet \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--address-prefix 10.0.2.0/24
az network public-ip create \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress
# Create the application gateway with WAF
az network application-gateway create \
--name myAppGateway \
--location eastus \
--resource-group myResourceGroupAG \
--vnet-name myVNet \
--subnet myAGSubnet \
--capacity 2 \
--sku WAF_Medium \
--http-settings-cookie-based-affinity Disabled \
--frontend-port 80 \
--http-settings-port 80 \
--http-settings-protocol Http \
--public-ip-address myAGPublicIPAddress
az network application-gateway waf-config set \
--enabled true \
--gateway-name myAppGateway \
--resource-group myResourceGroupAG \
--firewall-mode Detection \
--rule-set-version 3.0
# Create a virtual machine scale set
az vmss create \
--name myvmss \
--resource-group myResourceGroupAG \
--image Ubuntu2204 \
--admin-username azureuser \
--admin-password Azure123456! \
--instance-count 2 \
--vnet-name myVNet \
--subnet myBackendSubnet \
--vm-sku Standard_DS2 \
--upgrade-policy-mode Automatic \
--app-gateway myAppGateway \
--backend-pool-name appGatewayBackendPool
# Install NGINX
az vmss extension set \
--publisher Microsoft.Azure.Extensions \
--version 2.0 \
--name CustomScript \
--resource-group myResourceGroupAG \
--vmss-name myvmss \
--settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'
# Create a storage account
az storage account create \
--name myagstore1 \
--resource-group myResourceGroupAG \
--location eastus \
--sku Standard_LRS \
--encryption blob
# Configure diagnostics
appgwid=$(az network application-gateway show --name myAppGateway --resource-group myResourceGroupAG --query id -o tsv)
storeid=$(az storage account show --name myagstore1 --resource-group myResourceGroupAG --query id -o tsv)
az monitor diagnostic-settings create --name appgwdiag --resource $appgwid \
--logs '[ { "category": "ApplicationGatewayAccessLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } }, { "category": "ApplicationGatewayPerformanceLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } }, { "category": "ApplicationGatewayFirewallLog", "enabled": true, "retentionPolicy": { "days": 30, "enabled": true } } ]' \
--storage-account $storeid
# Get the IP address
az network public-ip show \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--query [ipAddress] \
--output tsv
整理部署
執行下列命令以移除資源群組、應用程式閘道和所有相關資源。
az group delete --name myResourceGroupAG --yes
腳本說明
此腳本會使用下列命令來建立部署。 表格中的每個項目都會連結到與該命令相關的文件。
指令 | 備註 |
---|---|
az group create (建立群組) | 建立用來存放所有資源的資源群組。 |
az network vnet create | 建立虛擬網路。 |
az network vnet 子網建立命令 | 在虛擬網路中建立子網路。 |
az network public-ip create | 建立應用程式閘道的公用IP位址。 |
az network application-gateway create(用於創建應用程序閘道器的命令) | 建立應用程式閘道。 |
az vmss create | 建立虛擬機器規模集。 |
az storage account create(建立儲存帳戶) | 建立儲存體帳戶。 |
az monitor diagnostic-settings create命令是用來建立診斷設定。 | 建立儲存體帳戶。 |
az network public-ip show | 取得應用程式閘道的公用 IP 位址。 |
後續步驟
如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 檔。