Share via


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

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

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

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

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

必要條件

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

建立自我簽署憑證

若要在生產環境中使用,您應該匯入由受信任提供者所簽署的有效憑證。 對於此教學課程,您會使用 openssl 命令建立自我簽署的憑證和 pfx 檔案。

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out appgwcert.crt

輸入對憑證有意義的值。 您可以接受預設值。

openssl pkcs12 -export -out appgwcert.pfx -inkey privateKey.key -in appgwcert.crt

輸入憑證的密碼。 此範例中使用的是 Azure123456!

建立資源群組

資源群組是在其中部署與管理 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

建立應用程式閘道

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

應用程式閘道會指派給您先前建立的 myAGSubnet 和 myAGPublicIPAddress。 在此範例中,您會在建立應用程式閘道時讓您建立的憑證與其密碼產生關聯。

az network application-gateway create \
  --name myAppGateway \
  --location eastus \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --subnet myAGsubnet \
  --capacity 2 \
  --sku Standard_Medium \
  --http-settings-cookie-based-affinity Disabled \
  --frontend-port 443 \
  --http-settings-port 80 \
  --http-settings-protocol Http \
  --public-ip-address myAGPublicIPAddress \
  --cert-file appgwcert.pfx \
  --cert-password "Azure123456!"

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

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

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

新增 HTTP 連接埠

您可以使用 az network application-gateway frontend-port create,將 HTTP 連接埠新增至應用程式閘道。

az network application-gateway frontend-port create \
  --port 80 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name httpPort

新增 HTTP 接聽程式

您可以使用 az network application-gateway http-listener create,將名為 myListener 的接聽程式新增至應用程式閘道。

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

新增重新導向設定

使用 az network application-gateway redirect-config create,將 HTTP 到 HTTPS 重新導向設定新增至應用程式閘道。

az network application-gateway redirect-config create \
  --name httpToHttps \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --type Permanent \
  --target-listener appGatewayHttpListener \
  --include-path true \
  --include-query-string true

新增路由規則

使用 az network application-gateway rule create,將名為 rule2 且具備重新導向設定的路由規則新增至應用程式閘道。

az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name rule2 \
  --resource-group myResourceGroupAG \
  --http-listener myListener \
  --rule-type Basic \
  --redirect-config httpToHttps

建立虛擬機器擴展集

在此範例中,您會建立一個名為 myvmss 的虛擬機器擴展集,以在應用程式閘道中提供後端集區的伺服器。 擴展集中的虛擬機器會與 myBackendSubnet 和 appGatewayBackendPool 相關聯。 若要建立擴展集,您可以使用 az vmss create

az vmss create \
  --name myvmss \
  --resource-group myResourceGroupAG \
  --image Ubuntu2204 \
  --admin-username azureuser \
  --admin-password Azure123456! \
  --instance-count 2 \
  --vnet-name myVNet \
  --subnet myBackendSubnet \
  --vm-sku Standard_DS2 \
  --upgrade-policy-mode Automatic \
  --app-gateway myAppGateway \
  --backend-pool-name appGatewayBackendPool

安裝 NGINX

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

測試應用程式閘道

若要取得應用程式閘道的公用 IP 位址,您可以使用 az network public-ip show。 將公用 IP 位址複製並貼到您瀏覽器的網址列。

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

Secure warning

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

Test base URL in application gateway

下一步