共用方式為


使用 Azure PowerShell 建立具有外部重新導向的應用程式閘道

您可以使用 Azure PowerShell,在建立應用程式閘道時設定 Web 流量重新導向。 在本教學課程中,您會設定接聽程式和規則,以將抵達應用程式閘道的 Web 流量重新導向至外部網站。

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

  • 設定網路
  • 建立接聽程式和重新導向規則
  • 建立應用程式閘道

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

注意

建議您使用 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 中。 Azure Cloud Shell 的「試試看」範例螢幕擷取畫面。
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 啟動 Azure Cloud Shell 的按鈕。
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 顯示 Azure 入口網站中 Cloud Shell 按鈕的螢幕擷取畫面

若要使用 Azure Cloud Shell:

  1. 啟動 Cloud Shell。

  2. 選取程式碼區塊 (或命令區塊) 上的 [複製] 按鈕以複製程式碼或命令。

  3. 透過在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼或命令貼到 Cloud Shell 工作階段中。

  4. 選取 Enter 鍵執行程式碼或命令。

如果您選擇在本機安裝和使用 PowerShell,本教學課程會要求使用 Azure PowerShell 模組 1.0.0 版或更新版本。 若要尋找版本,請執行 Get-Module -ListAvailable Az。 如果您需要升級,請參閱安裝 Azure PowerShell 模組。 如果正在本機執行 PowerShell,也需要執行 Login-AzAccount,以建立與 Azure 的連線。

建立資源群組

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

New-AzResourceGroup -Name myResourceGroupAG -Location eastus

建立網路資源

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

$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name myAGSubnet `
  -AddressPrefix 10.0.1.0/24
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myVNet `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $agSubnetConfig
$pip = New-AzPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myAGPublicIPAddress `
  -AllocationMethod Dynamic

建立應用程式閘道

建立 IP 設定與前端連接埠

使用 New-AzApplicationGatewayIPConfiguration,讓先前建立的 myAGSubnet 與應用程式閘道產生關聯。 使用 New-AzApplicationGatewayFrontendIPConfig,將公用 IP 位址指派給應用程式閘道。 然後,您可以使用 New-AzApplicationGatewayFrontendPort 來建立 HTTP 連接埠。

$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

建立後端集區和設定

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

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

建立接聽程式和規則

需要接聽程式才能讓應用程式閘道適當地路由傳送流量。 使用 New-AzApplicationGatewayHttpListener 搭配先前建立的前端設定和前端連接埠來建立接聽程式。 接聽程式需要規則才能知道要將傳入流量傳送到何處。 使用 New-AzApplicationGatewayRequestRoutingRule 建立名為 redirectRule 的基本規則。

$defaultListener = New-AzApplicationGatewayHttpListener `
  -Name defaultListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendport
$redirectConfig = New-AzApplicationGatewayRedirectConfiguration `
  -Name myredirect `
  -RedirectType Temporary `
  -TargetUrl "https://bing.com"
$redirectRule = New-AzApplicationGatewayRequestRoutingRule `
  -Name redirectRule `
  -RuleType Basic `
  -HttpListener $defaultListener `
  -RedirectConfiguration $redirectConfig

建立應用程式閘道

既然您已建立必要的支援資源,接著請使用 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 $redirectRule `
  -RedirectConfigurations $redirectConfig `
  -Sku $sku

測試應用程式閘道

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

Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

您應該會看到瀏覽器中出現 bing.com

下一步