使用 Azure PowerShell 限制 Web 流量
此脚本创建具有 Web 应用程序防火墙且使用虚拟机规模集作为后端服务器的应用程序网关。 Web 应用程序防火墙基于 OWASP 规则限制 Web 流量。 在运行脚本后,可以使用应用程序网关的公共 IP 地址测试该网关。
本示例需要 Azure PowerShell。 运行 Get-Module -ListAvailable Az
即可查找版本。
如果需要进行安装或升级,请参阅安装 Azure PowerShell 模块。
运行 Connect-AzAccount -Environment AzureChinaCloud cmdlet 以连接到世纪互联运营的 Azure。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
示例脚本
# Create a resource group
New-AzResourceGroup -Name myResourceGroupAG -Location chinanorth2
# Create network resources
$backendSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myBackendSubnet `
-AddressPrefix 10.0.1.0/24
$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myAGSubnet `
-AddressPrefix 10.0.2.0/24
$vnet = New-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Location chinanorth2 `
-Name myVNet `
-AddressPrefix 10.0.0.0/16 `
-Subnet $backendSubnetConfig, $agSubnetConfig
$pip = New-AzPublicIpAddress `
-ResourceGroupName myResourceGroupAG `
-Location chinanorth2 `
-Name myAGPublicIPAddress `
-AllocationMethod Dynamic
# Create IP configurations and frontend port
$vnet = Get-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Name myVNet
$subnet=$vnet.Subnets[0]
$gipconfig = New-AzApplicationGatewayIPConfiguration `
-Name myAGIPConfig `
-Subnet $subnet
$fipconfig = New-AzApplicationGatewayFrontendIPConfig `
-Name myAGFrontendIPConfig `
-PublicIPAddress $pip
$frontendport = New-AzApplicationGatewayFrontendPort `
-Name myFrontendPort `
-Port 80
# Create the backend pool and settings
$defaultPool = New-AzApplicationGatewayBackendAddressPool `
-Name appGatewayBackendPool
$poolSettings = New-AzApplicationGatewayBackendHttpSettings `
-Name myPoolSettings `
-Port 80 `
-Protocol Http `
-CookieBasedAffinity Enabled `
-RequestTimeout 120
# Create the default listener and rule
$defaultlistener = New-AzApplicationGatewayHttpListener `
-Name mydefaultListener `
-Protocol Http `
-FrontendIPConfiguration $fipconfig `
-FrontendPort $frontendport
$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
-Name rule1 `
-RuleType Basic `
-HttpListener $defaultlistener `
-BackendAddressPool $defaultPool `
-BackendHttpSettings $poolSettings
# Create the application gateway
$sku = New-AzApplicationGatewaySku `
-Name WAF_Medium `
-Tier WAF `
-Capacity 2
$wafConfig = New-AzApplicationGatewayWebApplicationFirewallConfiguration `
-Enabled $true `
-FirewallMode "Detection"
$appgw = New-AzApplicationGateway `
-Name myAppGateway `
-ResourceGroupName myResourceGroupAG `
-Location chinanorth2 `
-BackendAddressPools $defaultPool `
-BackendHttpSettingsCollection $poolSettings `
-FrontendIpConfigurations $fipconfig `
-GatewayIpConfigurations $gipconfig `
-FrontendPorts $frontendport `
-HttpListeners $defaultlistener `
-RequestRoutingRules $frontendRule `
-Sku $sku `
-WebApplicationFirewallConfig $wafConfig
# Create a virtual machine scale set
$vnet = Get-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Name myVNet
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
$backendPool = Get-AzApplicationGatewayBackendAddressPool `
-Name appGatewayBackendPool `
-ApplicationGateway $appgw
$ipConfig = New-AzVmssIpConfig `
-Name myVmssIPConfig `
-SubnetId $vnet.Subnets[1].Id `
-ApplicationGatewayBackendAddressPoolsId $backendPool.Id
$vmssConfig = New-AzVmssConfig `
-Location chinanorth2 `
-SkuCapacity 2 `
-SkuName Standard_DS2 `
-UpgradePolicyMode Automatic
Set-AzVmssStorageProfile $vmssConfig `
-ImageReferencePublisher MicrosoftWindowsServer `
-ImageReferenceOffer WindowsServer `
-ImageReferenceSku 2016-Datacenter `
-ImageReferenceVersion latest
Set-AzVmssOsProfile $vmssConfig `
-AdminUsername azureuser `
-AdminPassword "Azure123456!" `
-ComputerNamePrefix myvmss
Add-AzVmssNetworkInterfaceConfiguration `
-VirtualMachineScaleSet $vmssConfig `
-Name myVmssNetConfig `
-Primary $true `
-IPConfiguration $ipConfig
New-AzVmss `
-ResourceGroupName myResourceGroupAG `
-Name myvmss `
-VirtualMachineScaleSet $vmssConfig
# Install IIS
$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }
$vmss = Get-AzVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
-Name "customScript" `
-Publisher "Microsoft.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-Setting $publicSettings
Update-AzVmss `
-ResourceGroupName myResourceGroupAG `
-Name myvmss `
-VirtualMachineScaleSet $vmss
# Create a storage account
$storageAccount = New-AzStorageAccount `
-ResourceGroupName myResourceGroupAG `
-Name myagstore1 `
-Location chinanorth2 `
-SkuName "Standard_LRS"
# Configure diagnostics
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
$store = Get-AzStorageAccount `
-ResourceGroupName myResourceGroupAG `
-Name myagstore1
Set-AzDiagnosticSetting `
-ResourceId $appgw.Id `
-StorageAccountId $store.Id `
-Categories ApplicationGatewayAccessLog, ApplicationGatewayPerformanceLog, ApplicationGatewayFirewallLog `
-Enabled $true `
-RetentionEnabled $true `
-RetentionInDays 30
# Get the IP address
Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress
清理部署
运行以下命令来删除资源组、应用程序网关和所有相关资源。
Remove-AzResourceGroup -Name myResourceGroupAG
脚本说明
此脚本使用以下命令创建部署。 表中的每一项均链接到特定于命令的文档。
命令 | 注释 |
---|---|
New-AzResourceGroup | 创建用于存储所有资源的资源组。 |
New-AzVirtualNetworkSubnetConfig | 创建子网配置。 |
New-AzVirtualNetwork | 使用子网配置创建虚拟网络。 |
New-AzPublicIpAddress | 创建应用程序网关的公共 IP 地址。 |
New-AzApplicationGatewayIPConfiguration | 创建将子网与应用程序网关相关联的配置。 |
New-AzApplicationGatewayFrontendIPConfig | 创建为应用程序网关分配公共 IP 地址的配置。 |
New-AzApplicationGatewayFrontendPort | 分配用于访问应用程序网关的端口。 |
New-AzApplicationGatewayBackendAddressPool | 创建应用程序网关的后端池。 |
New-AzApplicationGatewayBackendHttpSettings | 配置后端池的设置。 |
New-AzApplicationGatewayHttpListener | 创建侦听器。 |
New-AzApplicationGatewayRequestRoutingRule | 创建路由规则。 |
New-AzApplicationGatewaySku | 指定应用程序网关的层和容量。 |
New-AzApplicationGatewayWebApplicationFirewallConfiguration | 创建 Web 应用程序防火墙配置。 |
New-AzApplicationGateway | 创建应用程序网关。 |
Set-AzVmssStorageProfile | 创建规模集的存储配置文件。 |
Set-AzVmssOsProfile | 定义规模集的操作系统。 |
Add-AzVmssNetworkInterfaceConfiguration | 定义规模集的网络接口。 |
New-AzVmss | 创建虚拟机规模集。 |
New-AzStorageAccount | 创建存储帐户。 |
Set-AzDiagnosticSetting | 配置诊断以记录数据。 |
Get-AzPublicIPAddress | 获取应用程序网关的公共 IP 地址。 |
Remove-AzResourceGroup | 删除资源组及其中包含的所有资源。 |
后续步骤
有关 Azure PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档。
可以在 Azure 应用程序网关文档中找到其他应用程序网关 PowerShell 脚本示例。