共用方式為


使用 Azure PowerShell 以建立具有 HTTP 到 HTTPS 重新導向功能的應用程式閘道

您可以使用 Azure PowerShell,透過 TLS/SSL 終止憑證建立應用程式閘道。 路由規則可用來將 HTTP 流量重新導向至您應用程式閘道中的 HTTPS 連接埠。 在此範例中,您也會為應用程式閘道的後端集區,建立一個包含兩個虛擬機器執行個體的虛擬機器擴展集

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

  • 建立自我簽署憑證
  • 設定網路
  • 建立包含憑證的應用程式閘道
  • 新增接聽程式和重新導向規則
  • 建立包含預設後端集區的虛擬機器擴展集

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

注意

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

本教學課程需要 Azure PowerShell 模組 1.0.0 版或更新版本。 執行 Get-Module -ListAvailable Az 以尋找版本。 如果您需要升級,請參閱安裝 Azure 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 Dynamic

建立應用程式閘道

建立 IP 設定與前端連接埠

使用 New-AzApplicationGatewayIPConfiguration,讓先前建立的 myAGSubnet 與應用程式閘道產生關聯。 使用 New-AzApplicationGatewayFrontendIPConfig,將 myAGPublicIPAddress 指派給應用程式閘道。 接著,您可以使用 New-AzApplicationGatewayFrontendPort 建立 HTTPS 連接埠。

$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 搭配前端組態、前端連接埠和先前建立的憑證,建立名為 appGatewayHttpListener 的接聽程式。 接聽程式需要規則以便知道要針對連入流量使用哪個後端集區。 使用 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 appGatewayHttpListener `
  -Protocol Https `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendPort `
  -SslCertificate $cert
$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
  -Name rule1 `
  -RuleType Basic `
  -HttpListener $defaultListener `
  -BackendAddressPool $defaultPool `
  -BackendHttpSettings $poolSettings

建立應用程式閘道

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

$sku = New-AzApplicationGatewaySku `
  -Name Standard_Medium `
  -Tier Standard `
  -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

新增接聽程式和重新導向規則

新增 HTTP 連接埠

使用 Add-AzApplicationGatewayFrontendPort 將 HTTP 連接埠新增至 應用程式閘道。

$appgw = Get-AzApplicationGateway `
  -Name myAppGateway `
  -ResourceGroupName myResourceGroupAG
Add-AzApplicationGatewayFrontendPort `
  -Name httpPort  `
  -Port 80 `
  -ApplicationGateway $appgw

新增 HTTP 接聽程式

使用 Add-AzApplicationGatewayHttpListener,將名為 myListener 的 HTTP 接聽程式新增至應用程式閘道。

$fipconfig = Get-AzApplicationGatewayFrontendIPConfig `
  -Name myAGFrontendIPConfig `
  -ApplicationGateway $appgw
$fp = Get-AzApplicationGatewayFrontendPort `
  -Name httpPort `
  -ApplicationGateway $appgw
Add-AzApplicationGatewayHttpListener `
  -Name myListener `
  -Protocol Http `
  -FrontendPort $fp `
  -FrontendIPConfiguration $fipconfig `
  -ApplicationGateway $appgw

新增重新導向設定

使用 Add-AzApplicationGatewayRedirectConfiguration,將 HTTP 到 HTTPS 重新導向設定新增至應用程式閘道。

$defaultListener = Get-AzApplicationGatewayHttpListener `
  -Name appGatewayHttpListener `
  -ApplicationGateway $appgw
Add-AzApplicationGatewayRedirectConfiguration -Name httpToHttps `
  -RedirectType Permanent `
  -TargetListener $defaultListener `
  -IncludePath $true `
  -IncludeQueryString $true `
  -ApplicationGateway $appgw

新增路由規則

使用 Add-AzApplicationGatewayRequestRoutingRule,將包含重新導向設定的路由規則新增至應用程式閘道。

$myListener = Get-AzApplicationGatewayHttpListener `
  -Name myListener `
  -ApplicationGateway $appgw
$redirectConfig = Get-AzApplicationGatewayRedirectConfiguration `
  -Name httpToHttps `
  -ApplicationGateway $appgw
Add-AzApplicationGatewayRequestRoutingRule `
  -Name rule2 `
  -RuleType Basic `
  -HttpListener $myListener `
  -RedirectConfiguration $redirectConfig `
  -ApplicationGateway $appgw
Set-AzApplicationGateway -ApplicationGateway $appgw

建立虛擬機器擴展集

在此範例中,您會建立虛擬機器擴展集,以在應用程式閘道中提供後端集區的伺服器。 當您設定 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 位址複製並貼到您瀏覽器的網址列。 例如,http://52.170.203.149

Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

安全警告

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

應用程式閘道中的測試基底URL

下一步