當您建立應用程式閘道時,您可以使用 Azure PowerShell 來設定 URL 型路由規則。 在本教學課程中,您可以使用虛擬機器擴展集來建立後端集區。 接著,您會建立 URL 路由規則,根據要求 URL 路徑,將 Web 流量重新導向至適當的後端集區。 當您建立應用程式閘道時,您可以使用 Azure PowerShell 來設定進階 URL 型路由規則。
本教學課程涵蓋生產就緒設定,包括安全性最佳做法、效能優化和監視設定。
在本教學課程中,您將瞭解如何:
- 設定網路基礎結構
- 使用路徑型路由建立應用程式閘道
- 新增 URL 重新導向的接聽程式和路由規則
- 為後端集區建立虛擬機器擴展集
- 測試應用程式閘道路由和重新導向功能
以下範例會顯示來自連接埠 8080 和 8081 並導向至相同後端集區的網站流量:
先決條件
如果您想要的話,可以使用 Azure CLI 完成此程序。
開始本教學課程之前,請確定您有:
- 有效的 Azure 訂用帳戶。 如果您沒有免費帳戶,請建立免費帳戶。
- 在本機安裝 Azure PowerShell 模組 5.4.1 版或更新版本,或存取 Azure Cloud Shell
- 目標 Azure 訂用帳戶的參與者或擁有者許可權
- 對應用程式閘道概念和 PowerShell 腳稿的基本瞭解
附註
建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 若要開始使用,請參閱安裝 Azure PowerShell。 若要了解如何遷移至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az。
Azure Cloud Shell
Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 您可以使用 Cloud Shell 預先安裝的命令,執行本文提到的程式碼,而不必在本機環境上安裝任何工具。
要啟動 Azure Cloud Shell:
| 選項 | 範例/連結 |
|---|---|
| 選取程式碼或命令區塊右上角的 [試試看]。 選取 [試試看] 並不會自動將程式碼或命令複製到 Cloud Shell 中。 |
|
| 前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 |
|
| 選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 |
|
若要使用 Azure Cloud Shell:
啟動 Cloud Shell。
選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。
透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。
選取 Enter 鍵執行程式碼或命令。
如果您選擇在本機安裝和使用 PowerShell,本教學課程需要 Azure PowerShell 模組 5.4.1 版或更新版本。 若要尋找版本,請執行 Get-Module -ListAvailable Az。 如果您需要升級,請參閱安裝 Azure PowerShell 模組。 如果您在本機執行 PowerShell,則也需要執行 Connect-AzAccount 以建立與 Azure 的連線。
建立資源群組
資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用 New-AzResourceGroup 來建立 Azure 資源群組。
# Define variables for consistent naming and easier management
$resourceGroupName = "myResourceGroupAG"
$location = "eastus"
# Create the resource group
New-AzResourceGroup -Name $resourceGroupName -Location $location
# Verify the resource group creation
Write-Output "Resource group '$resourceGroupName' created successfully in '$location'"
建立網路資源
使用 New-AzVirtualNetworkSubnetConfig 來建立 myBackendSubnet 和 myAGSubnet 的子網路設定。 使用 New-AzVirtualNetwork 搭配子網路設定來建立名為 myVNet 的虛擬網路。 最後,使用 New-AzPublicIpAddress 建立名為 myAGPublicIPAddress 的公用 IP 位址。 這些資源會提供應用程式閘道及其相關聯資源的網路連線能力。
這很重要
應用程式閘道子網 (myAGSubnet) 只能包含應用程式閘道。 在此子網中不允許使用任何其他資源。 使用 New-AzVirtualNetworkSubnetConfig 來建立 myBackendSubnet 和 myAGSubnet 的子網路設定。 應用程式閘道需要至少 /24 CIDR 的專用子網,才能適當運作並滿足未來的擴充需求。 使用 New-AzVirtualNetwork 搭配子網路設定來建立名為 myVNet 的虛擬網路。 最後,使用 Standard SKU 和靜態配置建立公用 IP 位址,以使用 New-AzPublicIpAddress 改善安全性和效能。
# Create backend subnet configuration with appropriate CIDR for scale sets
$backendSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myBackendSubnet `
-AddressPrefix 10.0.1.0/24
# Create Application Gateway subnet - dedicated subnet required
$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name myAGSubnet `
-AddressPrefix 10.0.2.0/24
New-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-Name myVNet `
-AddressPrefix 10.0.0.0/16 `
-Subnet $backendSubnetConfig, $agSubnetConfig
New-AzPublicIpAddress `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-Name myAGPublicIPAddress `
-AllocationMethod Dynamic
建立應用程式閘道
在本節中,您會建立支援應用程式閘道的資源,並在最後建立應用程式閘道。 您所建立的資源包括:
- IP 組態和前端連接埠 - 將您先前建立的子網路與應用程式閘道產生關聯,並且指派用來存取它的連接埠。
- 預設集區 - 所有應用程式閘道都必須具有至少一個伺服器後端集區。
- 預設接聽程式和規則 - 預設接聽程式會接聽所指派連接埠上的流量,而預設規則會將流量傳送到預設集區。
建立 IP 設定與前端連接埠
使用 New-AzApplicationGatewayIPConfiguration,讓先前建立的 myAGSubnet 與應用程式閘道產生關聯。 使用 New-AzApplicationGatewayFrontendIPConfig,將 myAGPublicIPAddress 指派給應用程式閘道。 然後使用 New-AzApplicationGatewayFrontendPort 建立 HTTP 埠。
# Get the virtual network and subnet references
$vnet = Get-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Name myVNet
$subnet=$vnet.Subnets[0]
# Get the public IP address
$pip = Get-AzPublicIpAddress `
-ResourceGroupName myResourceGroupAG `
-Name myAGPublicIPAddress
# Create IP configuration for the Application Gateway
$gipconfig = New-AzApplicationGatewayIPConfiguration `
-Name myAGIPConfig `
-Subnet $subnet
# Create frontend IP configuration
$fipconfig = New-AzApplicationGatewayFrontendIPConfig `
-Name myAGFrontendIPConfig `
-PublicIPAddress $pip
# Create frontend port for HTTP traffic
$frontendport = New-AzApplicationGatewayFrontendPort `
-Name myFrontendPort `
-Port 80
Write-Output "Application Gateway IP configurations created successfully"
建立預設集區和設定
使用 New-AzApplicationGatewayBackendAddressPool 為應用程式閘道建立名為 appGatewayBackendPool 的預設後端集區。 使用 New-AzApplicationGatewayBackendHttpSettings 來設定後端集區的設定。
# Create default backend pool
$defaultPool = New-AzApplicationGatewayBackendAddressPool `
-Name appGatewayBackendPool
# Create backend HTTP settings with optimized configuration
$poolSettings = New-AzApplicationGatewayBackendHttpSettings `
-Name myPoolSettings `
-Port 80 `
-Protocol Http `
-CookieBasedAffinity Enabled `
-RequestTimeout 120
建立預設接聽程式和規則
需要接聽元件,才能讓應用程式網關有效率地路由流量至後端資源池。 在本教學課程中,您會為不同的路由案例建立多個接聽程式。 第一個基本的接聽程式會預期位於根 URL 的流量。 其他接聽程式預期流量位於特定 URL 路徑,例如 http://203.0.113.1:8080/images/ 或 http://203.0.113.1:8081/video/。
使用 New-AzApplicationGatewayHttpListener 搭配您先前建立的前端設定和前端連接埠,來建立名為 defaultListener 的接聽程式。 接聽程式需要規則以便知道要針對連入流量使用哪個後端集區。 使用 New-AzApplicationGatewayRequestRoutingRule 來建立名為 rule1 的基本規則。
# Create default HTTP listener
$defaultlistener = New-AzApplicationGatewayHttpListener `
-Name defaultListener `
-Protocol Http `
-FrontendIPConfiguration $fipconfig `
-FrontendPort $frontendport
# Create basic routing rule that directs traffic to default pool
$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
-Name rule1 `
-RuleType Basic `
-HttpListener $defaultlistener `
-BackendAddressPool $defaultPool `
-BackendHttpSettings $poolSettings `
-Priority 100
Write-Output "Default listener and routing rule created successfully"
建立應用程式閘道
既然您已建立必要的支援資源,接著請使用 New-AzApplicationGatewaySku 為名為 myAppGateway 的應用程式閘道指定參數,然後使用 New-AzApplicationGateway 來建立它。
# Create SKU configuration for Application Gateway v2
$sku = New-AzApplicationGatewaySku `
-Name Standard_Medium `
-Tier Standard `
-Capacity 2
New-AzApplicationGateway `
-Name myAppGateway `
-ResourceGroupName myResourceGroupAG `
-Location eastus `
-BackendAddressPools $defaultPool `
-BackendHttpSettingsCollection $poolSettings `
-FrontendIpConfigurations $fipconfig `
-GatewayIpConfigurations $gipconfig `
-FrontendPorts $frontendport `
-HttpListeners $defaultlistener `
-RequestRoutingRules $frontendRule `
-Sku $sku
新增後端集區和連接埠
您可以使用 Add-AzApplicationGatewayBackendAddressPool 將後端集區新增至應用程式閘道。 在此範例中,建立 imagesBackendPool 和 videoBackendPool 用於路由特定內容類型。 您可以使用 Add-AzApplicationGatewayFrontendPort 為集區新增前埠。 使用 Set-AzApplicationGateway 將變更提交至應用程式閘道。
# Get the current Application Gateway configuration
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
# Add specialized backend pools for different content types
Add-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name imagesBackendPool
Add-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name videoBackendPool
# Add frontend ports for specialized listeners
Add-AzApplicationGatewayFrontendPort `
-ApplicationGateway $appgw `
-Name bport `
-Port 8080
Add-AzApplicationGatewayFrontendPort `
-ApplicationGateway $appgw `
-Name rport `
-Port 8081
# Apply the configuration changes
Set-AzApplicationGateway -ApplicationGateway $appgw
新增接聽程式和規則
新增接聽程式
使用 Add-AzApplicationGatewayHttpListener 來新增路由傳送流量時所需、名為 backendListener 和 redirectedListener 的接聽程式。
# Get the current Application Gateway configuration
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
# Get frontend port references
$backendPort = Get-AzApplicationGatewayFrontendPort `
-ApplicationGateway $appgw `
-Name bport
$redirectPort = Get-AzApplicationGatewayFrontendPort `
-ApplicationGateway $appgw `
-Name rport
# Get frontend IP configuration
$fipconfig = Get-AzApplicationGatewayFrontendIPConfig `
-ApplicationGateway $appgw
# Add listeners for different ports
Add-AzApplicationGatewayHttpListener `
-ApplicationGateway $appgw `
-Name backendListener `
-Protocol Http `
-FrontendIPConfiguration $fipconfig `
-FrontendPort $backendPort
Add-AzApplicationGatewayHttpListener `
-ApplicationGateway $appgw `
-Name redirectedListener `
-Protocol Http `
-FrontendIPConfiguration $fipconfig `
-FrontendPort $redirectPort
# Apply the configuration changes
Set-AzApplicationGateway -ApplicationGateway $appgw
新增預設 URL 路徑對應
URL 路徑對應可確保特定 URL 會路由傳送到特定的後端集區。 您可以使用 New-AzApplicationGatewayPathRuleConfig 和 Add-AzApplicationGatewayUrlPathMapConfig 來建立名為 imagePathRule 和 videoPathRule 的 URL 路徑對應。
# Get the current Application Gateway configuration
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
# Get backend HTTP settings
$poolSettings = Get-AzApplicationGatewayBackendHttpSettings `
-ApplicationGateway $appgw `
-Name myPoolSettings
# Get backend address pools
$imagePool = Get-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name imagesBackendPool
$videoPool = Get-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name videoBackendPool
$defaultPool = Get-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name appGatewayBackendPool
# Create path rules for different content types
$imagePathRule = New-AzApplicationGatewayPathRuleConfig `
-Name imagePathRule `
-Paths "/images/*" `
-BackendAddressPool $imagePool `
-BackendHttpSettings $poolSettings
$videoPathRule = New-AzApplicationGatewayPathRuleConfig `
-Name videoPathRule `
-Paths "/video/*" `
-BackendAddressPool $videoPool `
-BackendHttpSettings $poolSettings
# Add URL path map configuration
Add-AzApplicationGatewayUrlPathMapConfig `
-ApplicationGateway $appgw `
-Name urlpathmap `
-PathRules $imagePathRule, $videoPathRule `
-DefaultBackendAddressPool $defaultPool `
-DefaultBackendHttpSettings $poolSettings
# Apply the configuration changes
Set-AzApplicationGateway -ApplicationGateway $appgw
新增重新導向設定
您可以使用 Add-AzApplicationGatewayRedirectConfiguration,為接聽程式設定重新導向。
# Get the current Application Gateway configuration
$appgw = Get-AzApplicationGateway `
-ResourceGroupName $resourceGroupName `
-Name myAppGateway
# Get the target listener for redirection
$backendListener = Get-AzApplicationGatewayHttpListener `
-ApplicationGateway $appgw `
-Name backendListener
# Add redirection configuration with query string and path preservation
$redirectConfig = Add-AzApplicationGatewayRedirectConfiguration `
-ApplicationGateway $appgw `
-Name redirectConfig `
-RedirectType Found `
-TargetListener $backendListener `
-IncludePath $true `
-IncludeQueryString $true
# Apply the configuration changes
Set-AzApplicationGateway -ApplicationGateway $appgw
Write-Output "Redirection configuration added successfully"
Write-Output "Redirect type: HTTP 302 Found"
Write-Output "Target: backendListener (Port 8080)"
Write-Output "Preserves: Path and Query String"
新增重新導向 URL 路徑對應
針對重新導向情境建立個別的 URL 路徑映射。 此映射會處理埠 8081 上的流量,並將特定路徑重新導向至埠 8080 上的適當侦听器。
# Get the current Application Gateway configuration
$appgw = Get-AzApplicationGateway `
-ResourceGroupName $resourceGroupName `
-Name myAppGateway
# Get references to existing configurations
$poolSettings = Get-AzApplicationGatewayBackendHttpSettings `
-ApplicationGateway $appgw `
-Name myPoolSettings
$defaultPool = Get-AzApplicationGatewayBackendAddressPool `
-ApplicationGateway $appgw `
-Name appGatewayBackendPool
$redirectConfig = Get-AzApplicationGatewayRedirectConfiguration `
-ApplicationGateway $appgw `
-Name redirectConfig
# Create path rule for redirection - images traffic will be redirected
$redirectPathRule = New-AzApplicationGatewayPathRuleConfig `
-Name redirectPathRule `
-Paths "/images/*" `
-RedirectConfiguration $redirectConfig
# Add redirection path map configuration
Add-AzApplicationGatewayUrlPathMapConfig `
-ApplicationGateway $appgw `
-Name redirectpathmap `
-PathRules $redirectPathRule `
-DefaultBackendAddressPool $defaultPool `
-DefaultBackendHttpSettings $poolSettings
# Apply the configuration changes
Set-AzApplicationGateway -ApplicationGateway $appgw
Write-Output "Redirection URL path map added successfully"
Write-Output "Redirection rule: /images/* on port 8081 -> port 8080"
新增路由規則
路由規則會讓 URL 對應與您所建立的接聽程式產生關聯。 您可以使用 Add-AzApplicationGatewayRequestRoutingRule 來新增名為 defaultRule 和 redirectedRule 的規則。
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
$backendlistener = Get-AzApplicationGatewayHttpListener `
-ApplicationGateway $appgw `
-Name backendListener
$redirectlistener = Get-AzApplicationGatewayHttpListener `
-ApplicationGateway $appgw `
-Name redirectedListener
$urlPathMap = Get-AzApplicationGatewayUrlPathMapConfig `
-ApplicationGateway $appgw `
-Name urlpathmap
$redirectPathMap = Get-AzApplicationGatewayUrlPathMapConfig `
-ApplicationGateway $appgw `
-Name redirectpathmap
Add-AzApplicationGatewayRequestRoutingRule `
-ApplicationGateway $appgw `
-Name defaultRule `
-RuleType PathBasedRouting `
-HttpListener $backendlistener `
-UrlPathMap $urlPathMap
Add-AzApplicationGatewayRequestRoutingRule `
-ApplicationGateway $appgw `
-Name redirectedRule `
-RuleType PathBasedRouting `
-HttpListener $redirectlistener `
-UrlPathMap $redirectPathMap
Set-AzApplicationGateway -ApplicationGateway $appgw
建立虛擬機器擴展集
在此範例中,您要建立三個虛擬機器擴展集,以支援您所建立的三個後端集區。 擴展集的名稱為 myvmss1、 myvmss2 和 myvmss3。 每個擴展集都會包含兩個您安裝 IIS 的虛擬機器執行個體。 當您設定 IP 設定時,要將擴展集指派給後端集區。
這很重要
在執行腳本之前,請用您自己的值替換<username>和<password>。 使用符合 Azure 安全性需求的強密碼。
安全性注意事項:以安全認證取代 <username> 和 <password> 。 請考慮在生產案例中使用 Azure Key Vault 進行認證管理。
# Get network and Application Gateway references
$vnet = Get-AzVirtualNetwork `
-ResourceGroupName myResourceGroupAG `
-Name myVNet
$appgw = Get-AzApplicationGateway `
-ResourceGroupName myResourceGroupAG `
-Name myAppGateway
# Get backend pool references
$backendPool = Get-AzApplicationGatewayBackendAddressPool `
-Name appGatewayBackendPool `
-ApplicationGateway $appgw
$imagesPool = Get-AzApplicationGatewayBackendAddressPool `
-Name imagesBackendPool `
-ApplicationGateway $appgw
$videoPool = Get-AzApplicationGatewayBackendAddressPool `
-Name videoBackendPool `
-ApplicationGateway $appgw
# Create three scale sets with improved configuration
for ($i=1; $i -le 3; $i++)
{
if ($i -eq 1)
{
$poolId = $backendPool.Id
}
if ($i -eq 2)
{
$poolId = $imagesPool.Id
}
if ($i -eq 3)
{
$poolId = $videoPool.Id
}
$ipConfig = New-AzVmssIpConfig `
-Name myVmssIPConfig$i `
-SubnetId $vnet.Subnets[1].Id `
-ApplicationGatewayBackendAddressPoolsId $poolId
# Create scale set configuration with modern VM size and settings
$vmssConfig = New-AzVmssConfig `
-Location eastus `
-SkuCapacity 2 `
-SkuName Standard_DS2 `
-UpgradePolicyMode Automatic
# Configure storage profile with Windows Server 2022
Set-AzVmssStorageProfile $vmssConfig `
-ImageReferencePublisher MicrosoftWindowsServer `
-ImageReferenceOffer WindowsServer `
-ImageReferenceSku 2016-Datacenter `
-ImageReferenceVersion latest `
-OsDiskCreateOption FromImage
Set-AzVmssOsProfile $vmssConfig `
-AdminUsername <username> `
-AdminPassword "<password>" `
-ComputerNamePrefix myvmss$i
# Add network interface configuration
Add-AzVmssNetworkInterfaceConfiguration `
-VirtualMachineScaleSet $vmssConfig `
-Name myVmssNetConfig$i `
-Primary $true `
-IPConfiguration $ipConfig
New-AzVmss `
-ResourceGroupName myResourceGroupAG `
-Name myvmss$i `
-VirtualMachineScaleSet $vmssConfig
Write-Output "Virtual Machine Scale Set myvmss$i created successfully"
}
Write-Output "All Virtual Machine Scale Sets created successfully"
安裝 IIS
下列腳本會在每個擴展集中的虛擬機上安裝 IIS,並設定它們根據它們所服務的後端集區顯示不同的內容。
$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/appgatewayurl.ps1");
"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }
# Install IIS on all scale sets
for ($i=1; $i -le 3; $i++)
{
$vmss = Get-AzVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss$i
Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
-Name "customScript" `
-Publisher "Microsoft.Compute" `
-Type "CustomScriptExtension" `
-TypeHandlerVersion 1.8 `
-Setting $publicSettings
Update-AzVmss `
-ResourceGroupName myResourceGroupAG `
-Name myvmss$i `
-VirtualMachineScaleSet $vmss
}
測試應用程式閘道
雖然不需要 IIS 才能建立應用程式閘道,但您已在本教學課程中安裝它,以確認 Azure 是否已成功建立應用程式閘道。 使用 IIS 測試應用程式閘道:
執行 Get-AzPublicIPAddress 以取得應用程式閘道的公用 IP 位址:
Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress將公用 IP 位址複製並貼到您瀏覽器的網址列。 例如:
-
http://203.0.113.1(基底 URL) -
http://203.0.113.1:8080/images/test.htm(圖片路徑) -
http://203.0.113.1:8080/video/test.htm(視頻路徑) -
http://203.0.113.1:8081/images/test.htm(重新導向測試)
-
將 URL 變更為 http://<ip-address>:8080/images/test.htm,以 取代您的 IP 位址 <ip-address>,您應該會看到類似下列範例的內容:
將 URL 變更為 http://<ip-address>:8080/video/test.htm,以 取代您的 IP 位址 <ip-address>,您應該會看到類似下列範例的內容:
現在,將 URL 變更為 http://<ip-address>:8081/images/test.htm,並將您的 IP 位址替換為 <ip-address>,這樣您應該會看到流量被重新導向到圖像後端集區 http://<ip-address>:8080/images。
效能監控
監視主要應用程式閘道計量以獲得最佳效能:
- 要求計數:已處理的要求總數
- 回應時間:要求的平均回應時間
- 不健康的主機數目:不健康的後端伺服器數量
- 吞吐量:透過應用程式閘道的資料傳輸速率
清除資源
當不再需要資源群組、應用程式閘道及所有相關資源時,請使用 Remove-AzResourceGroup 來將其移除。
Remove-AzResourceGroup -Name myResourceGroupAG
後續步驟
既然您已瞭解應用程式閘道的 URL 路徑型重新導向,請探索下列進階案例: