教學課程:使用 Azure CLI 建立包含 URL 路徑型重新導向的應用程式閘道

您可以使用 Azure CLI,在建立應用程式閘道時設定 URL 路徑型路由規則。 在本教學課程中,您可以使用虛擬機器擴展集來建立後端集區。 然後,您可以建立 URL 路由規則,確保 Web 流量會重新導向到適當的後端集區。

在本教學課程中,您會了解如何:

  • 設定網路
  • 建立應用程式閘道
  • 新增接聽程式和路由規則
  • 為後端集區建立虛擬機器擴展集

以下範例會顯示來自連接埠 8080 和 8081 並導向至相同後端集區的網站流量:

URL routing example

您可以使用 Azure PowerShell 完成本教學課程。

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本教學課程需要 2.0.4 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立資源群組

資源群組是在其中部署與管理 Azure 資源的邏輯容器。 使用 az group create 建立資源群組。

下列範例會在 eastus 位置建立名為 myResourceGroupAG 的資源群組。

az group create --name myResourceGroupAG --location eastus

建立網路資源

使用 az network vnet create 建立名為 myVNet 的虛擬網路,以及名為 myAGSubnet 的子網路。 然後您可以使用 az network vnet subnet create 新增名為 myBackendSubnet 的子網路,後端伺服器需要該子網路。 使用 az network public-ip create 建立名為 myAGPublicIPAddress 的公用 IP 位址。

az network vnet create \
  --name myVNet \
  --resource-group myResourceGroupAG \
  --location eastus \
  --address-prefix 10.0.0.0/16 \
  --subnet-name myAGSubnet \
  --subnet-prefix 10.0.1.0/24

az network vnet subnet create \
  --name myBackendSubnet \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --address-prefix 10.0.2.0/24

az network public-ip create \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --allocation-method Static \
  --sku Standard

建立應用程式閘道

使用 az network application-gateway create,建立名為 myAppGateway 的應用程式閘道。 當您使用 Azure CLI 建立應用程式閘道時,需要指定設定資訊,例如容量、SKU 和 HTTP 設定。 應用程式閘道會指派給您先前建立的 myAGSubnet 和 myPublicIPAddress

az network application-gateway create \
  --name myAppGateway \
  --location eastus \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --subnet myAGsubnet \
  --capacity 2 \
  --sku Standard_v2 \
  --http-settings-cookie-based-affinity Disabled \
  --frontend-port 80 \
  --http-settings-port 80 \
  --http-settings-protocol Http \
  --public-ip-address myAGPublicIPAddress \
  --priority 100

可能需要幾分鐘的時間來建立應用程式閘道。 建立應用程式閘道後,您可以看到這些新功能:

  • appGatewayBackendPool - 應用程式閘道必須至少有一個後端位址集區。
  • appGatewayBackendHttpSettings - 指定以連接埠 80 和 HTTP 通訊協定來進行通訊。
  • appGatewayHttpListener - 與 appGatewayBackendPool 相關聯的預設接聽程式。
  • appGatewayFrontendIP - 將 myAGPublicIPAddress 指派給 appGatewayHttpListener
  • rule1 - 與 appGatewayHttpListener 相關聯的預設路由規則。

新增後端集區和連接埠

您可以使用 az network application-gateway address-pool create,將名為 imagesBackendPoolvideoBackendPool 的後端位址集區新增至應用程式閘道。 您可以使用 az network application-gateway frontend-port create,以新增集區的前端連接埠。

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name imagesBackendPool

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name videoBackendPool

az network application-gateway frontend-port create \
  --port 8080 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name bport

az network application-gateway frontend-port create \
  --port 8081 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name rport

新增接聽程式和規則

新增接聽程式

使用 az network application-gateway http-listener create,以新增路由流量時所需且名為 backendListenerredirectedListener 的後端接聽程式。

az network application-gateway http-listener create \
  --name backendListener \
  --frontend-ip appGatewayFrontendIP \
  --frontend-port bport \
  --resource-group myResourceGroupAG \
  --gateway-name myAppGateway

az network application-gateway http-listener create \
  --name redirectedListener \
  --frontend-ip appGatewayFrontendIP \
  --frontend-port rport \
  --resource-group myResourceGroupAG \
  --gateway-name myAppGateway

新增預設 URL 路徑對應

URL 路徑對應可確保特定 URL 會路由到特定的後端集區。 您可以使用 az network application-gateway url-path-map createaz network application-gateway url-path-map rule create,建立名為 imagePathRule 和 videoPathRule 的 URL 路徑對應

az network application-gateway url-path-map create \
  --gateway-name myAppGateway \
  --name urlpathmap \
  --paths /images/* \
  --resource-group myResourceGroupAG \
  --address-pool imagesBackendPool \
  --default-address-pool appGatewayBackendPool \
  --default-http-settings appGatewayBackendHttpSettings \
  --http-settings appGatewayBackendHttpSettings \
  --rule-name imagePathRule

az network application-gateway url-path-map rule create \
  --gateway-name myAppGateway \
  --name videoPathRule \
  --resource-group myResourceGroupAG \
  --path-map-name urlpathmap \
  --paths /video/* \
  --address-pool videoBackendPool

新增重新導向設定

您可以使用 az network application-gateway redirect-config create,以設定接聽程式的重新導向。

az network application-gateway redirect-config create \
  --gateway-name myAppGateway \
  --name redirectConfig \
  --resource-group myResourceGroupAG \
  --type Found \
  --include-path true \
  --include-query-string true \
  --target-listener backendListener

新增重新導向 URL 路徑對應

az network application-gateway url-path-map create \
  --gateway-name myAppGateway \
  --name redirectpathmap \
  --paths /images/* \
  --resource-group myResourceGroupAG \
  --redirect-config redirectConfig \
  --rule-name redirectPathRule

新增路由規則

路由規則會讓 URL 路徑對應與您所建立的接聽程式產生關聯。 您可以使用 az network application-gateway rule create,以新增名為 defaultRuleredirectedRule 的規則。

az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name defaultRule \
  --resource-group myResourceGroupAG \
  --http-listener backendListener \
  --rule-type PathBasedRouting \
  --url-path-map urlpathmap \
  --address-pool appGatewayBackendPool \
  --priority 100

az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name redirectedRule \
  --resource-group myResourceGroupAG \
  --http-listener redirectedListener \
  --rule-type PathBasedRouting \
  --url-path-map redirectpathmap \
  --address-pool appGatewayBackendPool \
  --priority 100

建立虛擬機器擴展集

在此範例中,您要建立三個虛擬機器擴展集,以支援您所建立的三個後端集區。 您所建立的擴展集名為 myvmss1、myvmss2 和 myvmss3。 每個擴展集都會包含兩個您安裝 NGINX 的虛擬機器執行個體。

以您選擇的使用者名稱和密碼取代 <azure 使用者>和<密碼>。

for i in `seq 1 3`; do
  if [ $i -eq 1 ]
  then
    poolName="appGatewayBackendPool"
  fi
  if [ $i -eq 2 ]
  then
    poolName="imagesBackendPool"
  fi
  if [ $i -eq 3 ]
  then
    poolName="videoBackendPool"
  fi

  az vmss create \
    --name myvmss$i \
    --resource-group myResourceGroupAG \
    --image Ubuntu2204 \
    --admin-username <azure-user> \
    --admin-password <password> \
    --instance-count 2 \
    --vnet-name myVNet \
    --subnet myBackendSubnet \
    --vm-sku Standard_DS2 \
    --upgrade-policy-mode Automatic \
    --app-gateway myAppGateway \
    --backend-pool-name $poolName
done

安裝 NGINX

for i in `seq 1 3`; do
  az vmss extension set \
    --publisher Microsoft.Azure.Extensions \
    --version 2.0 \
    --name CustomScript \
    --resource-group myResourceGroupAG \
    --vmss-name myvmss$i \
    --settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'

done

測試應用程式閘道

若要取得應用程式閘道的公用 IP 位址,請使用 az network public-ip show。 將公用 IP 位址複製並貼到您瀏覽器的網址列。 例如 http://40.121.222.19http://40.121.222.19:8080/images/test.htmhttp://40.121.222.19:8080/video/test.htmhttp://40.121.222.19:8081/images/test.htm

az network public-ip show \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --query [ipAddress] \
  --output tsv

Test base URL in application gateway

將 URL 變更為 http://<ip-address>:8080/images/test.html、使用您的 IP 位址取代 <ip-address>,您應該會看到類似下列的範例:

Test images URL in application gateway

將 URL 變更為 http://<ip-address>:8080/video/test.html、使用您的 IP 位址取代 <ip-address>,您應該會看到類似下列的範例:

Test video URL in application gateway

現在,請將 URL 變更為 http://<ip-address>:8081/images/test.htm、使用您的 IP 位址取代 <ip-address>,然後就會看到流量重新導向回位於 http://<ip-address>:8080/images 的影像後端集區。

清除資源

若不再需要,可移除資源群組、應用程式閘道和所有相關資源。

az group delete --name myResourceGroupAG

下一步