Share via


使用 Azure PowerShell 建立包含 TLS 終止的應用程式閘道

您可以使用 Azure PowerShell 來建立應用程式閘道,當中包含使用虛擬機器擴展集作為後端伺服器的 TLS/SSL 終止憑證。 在此範例中,該擴展集包含兩個虛擬機器執行個體,這些執行個體會新增至應用程式閘道的預設後端集區。

在本文中,您將學會如何:

  • 建立自我簽署憑證
  • 設定網路
  • 建立包含憑證的應用程式閘道
  • 建立包含預設後端集區的虛擬機器擴展集

如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶

注意

建議您使用 Azure Az PowerShell 模組來與 Azure 互動。 請參閱安裝 Azure PowerShell 以開始使用。 若要了解如何移轉至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 移轉至 Az

本文需要 Azure PowerShell 模組 1.0.0 版或更新版本。 執行 Get-Module -ListAvailable Az 以尋找版本。 如果您需要升級,請參閱安裝 Azure PowerShell 模組。 如果正在本機執行 PowerShell,也需要執行 Login-AzAccount,以建立與 Azure 的連線。

建立自我簽署憑證

若要在生產環境中使用,您應該匯入由受信任提供者所簽署的有效憑證。 在本文中,您要使用 New-selfsignedcertificate 建立自我簽署憑證。 您可以使用 Export-PfxCertificate 搭配系統所傳回的指紋,以將 pfx 檔案從憑證匯出。

New-SelfSignedCertificate `
  -certstorelocation cert:\localmachine\my `
  -dnsname www.contoso.com

您應該會看到類似這個結果的內容:

PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\my

Thumbprint                                Subject
----------                                -------
E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630  CN=www.contoso.com

使用指紋來建立 pfx 檔案:

$pwd = ConvertTo-SecureString -String "Azure123456!" -Force -AsPlainText

Export-PfxCertificate `
  -cert cert:\localMachine\my\E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630 `
  -FilePath c:\appgwcert.pfx `
  -Password $pwd

建立資源群組

資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用 New-AzResourceGroup 建立名為 myResourceGroupAG 的 Azure 資源群組。

New-AzResourceGroup -Name myResourceGroupAG -Location eastus

建立網路資源

使用 New-AzVirtualNetworkSubnetConfig 來設定名為 myBackendSubnetmyAGSubnet 的子網路。 使用 New-AzVirtualNetwork 搭配子網路設定來建立名為 myVNet 的虛擬網路。 最後,使用 New-AzPublicIpAddress 來建立名為 myAGPublicIPAddress 的公用 IP 位址。 這些資源可用來為應用程式閘道及其相關聯的資源提供網路連線。

$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 eastus `
  -Name myVNet `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $backendSubnetConfig, $agSubnetConfig

$pip = New-AzPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myAGPublicIPAddress `
  -AllocationMethod Static `
  -Sku Standard

建立應用程式閘道

建立 IP 設定與前端連接埠

使用 New-AzApplicationGatewayIPConfiguration,讓先前建立的 myAGSubnet 與應用程式閘道產生關聯。 使用 New-AzApplicationGatewayFrontendIPConfig,將 myAGPublicIPAddress 指派給應用程式閘道。

$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 443

建立後端集區和設定

使用 New-AzApplicationGatewayBackendAddressPool,為應用程式閘道建立名為 appGatewayBackendPool 的後端集區。 使用 New-AzApplicationGatewayBackendHttpSettings 來設定後端集區的設定。

$defaultPool = New-AzApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool

$poolSettings = New-AzApplicationGatewayBackendHttpSettings `
  -Name myPoolSettings `
  -Port 80 `
  -Protocol Http `
  -CookieBasedAffinity Enabled `
  -RequestTimeout 120

建立預設接聽程式和規則

需要接聽程式才能讓應用程式閘道將流量適當地路由到後端集區。 在此範例中,您會建立基本接聽程式,接聽根 URL 的 HTTPS 流量。

使用 New-AzApplicationGatewaySslCertificate 建立憑證物件,然後使用 New-AzApplicationGatewayHttpListener 搭配前端組態、前端連接埠和先前建立的憑證,建立名為 mydefaultListener 的接聽程式。 接聽程式需要規則以便知道要針對連入流量使用哪個後端集區。 使用 New-AzApplicationGatewayRequestRoutingRule 來建立名為 rule1 的基本規則。

$pwd = ConvertTo-SecureString `
  -String "Azure123456!" `
  -Force `
  -AsPlainText

$cert = New-AzApplicationGatewaySslCertificate `
  -Name "appgwcert" `
  -CertificateFile "c:\appgwcert.pfx" `
  -Password $pwd

$defaultlistener = New-AzApplicationGatewayHttpListener `
  -Name mydefaultListener `
  -Protocol Https `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendport `
  -SslCertificate $cert

$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
  -Name rule1 `
  -RuleType Basic `
  -HttpListener $defaultlistener `
  -BackendAddressPool $defaultPool `
  -BackendHttpSettings $poolSettings `
  -priority 100

建立包含憑證的應用程式閘道

現在您已建立必要的支援資源,請使用 New-AzApplicationGatewaySku 為名為 myAppGateway 的應用程式閘道指定參數,然後使用 New-AzApplicationGateway 搭配憑證加以建立。

建立應用程式閘道

$sku = New-AzApplicationGatewaySku `
  -Name Standard_v2 `
  -Tier Standard_v2 `
  -Capacity 2

$appgw = New-AzApplicationGateway `
  -Name myAppGateway `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -BackendAddressPools $defaultPool `
  -BackendHttpSettingsCollection $poolSettings `
  -FrontendIpConfigurations $fipconfig `
  -GatewayIpConfigurations $gipconfig `
  -FrontendPorts $frontendport `
  -HttpListeners $defaultlistener `
  -RequestRoutingRules $frontendRule `
  -Sku $sku `
  -SslCertificates $cert

建立虛擬機器擴展集

在此範例中,您會建立虛擬機器擴展集,以在應用程式閘道中提供後端集區的伺服器。 當您設定 IP 設定時,要將擴展集指派給後端集區。

$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 eastus `
  -SkuCapacity 2 `
  -SkuName Standard_DS2 `
  -UpgradePolicyMode Automatic

Set-AzVmssStorageProfile $vmssConfig `
  -ImageReferencePublisher MicrosoftWindowsServer `
  -ImageReferenceOffer WindowsServer `
  -ImageReferenceSku 2016-Datacenter `
  -ImageReferenceVersion latest `
  -OsDiskCreateOption FromImage

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

安裝 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" }

$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

測試應用程式閘道

您可使用 Get-AzPublicIPAddress 來取得應用程式閘道的公用 IP 位址。 將公用 IP 位址複製並貼到您瀏覽器的網址列。

Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

Secure warning

若要在使用自我簽署憑證時接受安全性警告,請依序按一下 [詳細資料] 與 [繼續瀏覽網頁]。 接著會顯示受保護的 IIS 網站,如下列範例所示:

Test base URL in application gateway

清除資源

當不再需要資源群組、應用程式閘道及所有相關資源時,請使用 Remove-AzResourceGroup 來將其移除。

Remove-AzResourceGroup -Name myResourceGroupAG

下一步

建立裝載多個網站的應用程式閘道