適用於: ✔️ Front Door (傳統)
- 從 2025 年 8 月 15 日起,Azure Front Door(傳統版)將不再支援受控憑證。 若要避免服務中斷,請 切換至 「攜帶您自己的憑證」, 或在 2025 年 8 月 15 日前移轉至 AFD 標準和進階 。 現有的受控憑證將在 2025 年 8 月 15 日之前自動更新,並持續有效到 2026 年 4 月 14 日為止。 瞭解更多資訊
- Azure Front Door (傳統) 將於 2027 年 3 月 31 日淘汰。 若要避免服務中斷,請移轉至 AFD Standard 或 Premium。 深入瞭解。
此 Azure CLI 指令碼範例示範如何在 Azure Front Door 前端上部署自訂網域名稱與 TLS 憑證。 此指令碼會使用自訂網域名稱 (由 Azure DNS 裝載) 與 TLS 憑證,自動佈建 Azure Front Door。
Important
請確定您的網域名稱名稱已有 Azure DNS 公用區域存在。 如需教學課程,請參閱在 Azure DNS 中裝載您的網域
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
Prerequisites
在 Azure Cloud Shell 中使用 Bash 環境。 如需詳細資訊,請參閱開始使用 Azure Cloud Shell。
若要在本機執行 CLI 參考命令,請安裝 (部分機器翻譯) Azure CLI。 若您在 Windows 或 macOS 上執行,請考慮在 Docker 容器中執行 Azure CLI。 如需詳細資訊,請參閱如何在 Docker 容器中執行 Azure CLI。
如果您使用的是本機安裝,請使用 az login 命令,透過 Azure CLI 來登入。 請遵循您終端機上顯示的步驟,以完成驗證流程。 如需其他登入選項,請參閱 使用 Azure CLI 向 Azure 進行驗證。
出現提示時,請在第一次使用時安裝 Azure CLI 延伸模組。 如需擴充功能的詳細資訊,請參閱 使用和管理 Azure CLI 的擴充功能。
執行 az version (部分機器翻譯) 以尋找已安裝的版本和相依程式庫。 若要升級至最新版本,請執行 az upgrade。
範例腳本
啟動 Azure Cloud Shell
Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。
若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。 您也可以移至 https://shell.azure.com,從另一個瀏覽器索引標籤啟動 Cloud Shell。
當開啟 Cloud Shell 時,請確認已為您的環境選取 Bash。 後續的工作階段將會在 Bash 環境中使用 Azure CLI,請選取 [複製] 以複製程式碼區塊,並將其貼到 Cloud Shell 中,然後按 Enter 鍵加以執行。
登入 Azure
系統會在登入的初始帳戶下自動驗證 Cloud Shell。 使用下列指令碼使用不同的訂用帳戶登入,並將 subscriptionId 取代為您的 Azure 訂用帳戶識別碼。
如果您沒有 Azure 帳戶,請在開始之前建立 免費帳戶 。
subscription="subscriptionId" # Set Azure subscription ID here
az account set -s $subscription # ...or use 'az login'
如需詳細資訊,請參閱設定使用中訂用帳戶 (部分機器翻譯) 或以互動方式登入 (部分機器翻譯)。
入門指南
該指令碼會:
- 建立資源群組。
- 建立儲存體帳戶來裝載單頁應用程式 (SPA)。
- 在儲存體帳戶上啟用 SPA 裝載。
- 上傳 "Hello world!"
index.html檔案。 - 建立 Front Door 設定檔。
- 為解析為 Front Door 的 Apex 建立 DNS 別名。
- 建立
adverify主機名稱的 CNAME。 - 建立自訂網域的 Front Door 前端端點。
- 將路由從自訂網域前端新增至 SPA 來源。
- 新增路由規則以將 HTTP 重新導向至 HTTPS。
- 使用 Front Door 受控憑證啟用 HTTPS。
執行指令碼
若要執行此指令碼,請將下列程式碼複製到 .sh 檔案、將硬式編碼的變數變更為您的網域值,然後執行下列命令,將這些變數傳遞至指令碼:
AZURE_DNS_ZONE_NAME=www.contoso.com AZURE_DNS_ZONE_RESOURCE_GROUP=contoso-rg ./deploy-custom-apex-domain.sh
# Deploy a Custom Domain name and TLS certificate at the apex (root) on an Azure Front Door front-end.
# VARIABLES
# Change these hardcoded values if required
let "randomIdentifier=$RANDOM*$RANDOM"
# Use resource group environment variable if set
if [ "$RESOURCE_GROUP" == '' ];
then
resourceGroup="msdocs-frontdoor-rg-$randomIdentifier"
else
resourceGroup="${RESOURCE_GROUP}"
fi
location='AustraliaEast'
tag='deploy-custom-domain'
storage="msdocsafd$randomIdentifier"
frontDoor="msdocs-frontdoor-$randomIdentifier"
frontDoorFrontEnd='www-contoso'
ttl=300
if [ "$AZURE_DNS_ZONE_NAME" == '' ];
then
echo -e "\033[33mAZURE_DNS_ZONE_NAME environment variable is not set. Front Door will be created but custom frontend will not be configured because custom domain name not provided. Try:\n\n AZURE_DNS_ZONE_NAME=www.contoso.com AZURE_DNS_ZONE_RESOURCE_GROUP=contoso-dns-rg ./deploy-custom-apex-domain.sh\n\nSee Readme for details.\033[0m"
else
if [ "$AZURE_DNS_ZONE_RESOURCE_GROUP" == '' ];
then
# write error text
echo -e "\033[31mAZURE_DNS_ZONE_RESOURCE_GROUP environment variable is not set. Provide the resource group for the Azure DNS Zone. Try:\n\n AZURE_DNS_ZONE_NAME=www.contoso.com AZURE_DNS_ZONE_RESOURCE_GROUP=contoso-dns-rg ./deploy-custom-apex-domain.sh\n\nSee Readme for details.\033[0m"
# write stderr and exit
>&2 echo "AZURE_DNS_ZONE_RESOURCE_GROUP environment variable is not set."
exit 1
fi
fi
# Resource group
az group create -n $resourceGroup -l $location --tags $tag
# STORAGE ACCOUNT
az storage account create -n $storage -g $resourceGroup -l $location --sku Standard_LRS --kind StorageV2
# Make Storage Account a SPA
az storage blob service-properties update --account-name $storage --static-website \
--index-document 'index.html' --404-document 'index.html'
# Upload index.html
az storage blob upload --account-name $storage -f ./index.html -c '$web' -n 'index.html' --content-type 'text/html'
# Get the URL to use as the origin URL on the Front Door backend
spaFQUrl=$( az storage account show -n $storage --query 'primaryEndpoints.web' -o tsv )
# Remove 'https://' and trailing '/'
spaUrl=${spaFQUrl/https:\/\//} ; spaUrl=${spaUrl/\//}
# FRONT DOOR
frontDoorId=$( az network front-door create -n $frontDoor -g $resourceGroup --tags $tag --accepted-protocols Http Https --backend-address $spaUrl --query 'id' -o tsv )
if [ "$AZURE_DNS_ZONE_NAME" != '' ];
then
# AZURE DNS
# Apex hostname on contoso.com
# Create an Alias DNS recordset
az network dns record-set a create -n "@" -g $AZURE_DNS_ZONE_RESOURCE_GROUP --zone-name $AZURE_DNS_ZONE_NAME --target-resource $frontDoorId --ttl $ttl
# Create the domain verify CNAME
az network dns record-set cname set-record -g $AZURE_DNS_ZONE_RESOURCE_GROUP --zone-name $AZURE_DNS_ZONE_NAME --record-set-name "afdverify" --cname "afdverify.$frontDoor.azurefd.net" --ttl $ttl
# FRONT DOOR FRONT END
# Create a frontend for the custom domain
az network front-door frontend-endpoint create --front-door-name $frontDoor --host-name $AZURE_DNS_ZONE_NAME \
--name $frontDoorFrontEnd -g $resourceGroup --session-affinity-enabled 'Disabled'
# Update the default routing rule to include the new frontend
az network front-door routing-rule update --front-door-name $frontDoor -n 'DefaultRoutingRule' -g $resourceGroup \
--caching 'Enabled' --accepted-protocols 'Https' \
--frontend-endpoints 'DefaultFrontendEndpoint' $frontDoorFrontEnd
# Create http redirect to https routing rule
az network front-door routing-rule create -f $frontDoor -g $resourceGroup -n 'httpRedirect' \
--frontend-endpoints $frontDoorFrontEnd --accepted-protocols 'Http' --route-type 'Redirect' \
--patterns '/*' --redirect-protocol 'HttpsOnly'
# Update the default routing rule to include the new frontend
az network front-door routing-rule update --front-door-name $frontDoor -n 'DefaultRoutingRule' -g $resourceGroup \
--caching 'Enabled' --frontend-endpoints 'DefaultFrontendEndpoint' $frontDoorFrontEnd
# Enable HTTPS. This command will return quickly but provisioning can take up to an hour to complete
az network front-door frontend-endpoint enable-https \
--front-door-name $frontDoor -n $frontDoorFrontEnd -g $resourceGroup
fi
清除資源
使用下列命令來移除資源群組及所有與其相關聯的資源,除非您持續需要這些資源,則請使用 az group delete 命令。 這其中某些資源可能需要一些時間才能建立和刪除。
az group delete --name $resourceGroup
範例參考
此指令碼會使用下列命令。 下表中的每個命令都會連結至命令特定的文件。
| Command | Description |
|---|---|
| 請輸入命令:az group create 來建立群組。 | 建立資源群組來儲存所有資源。 |
| az storage account create(建立儲存帳戶) | 在指定的資源群組中建立 Azure 儲存體帳戶。 |
| az storage blob 服務屬性更新 | 更新儲存體 Blob 服務屬性。 |
| az storage blob upload | 將 Blob 上傳至容器。 |
| az 儲存帳戶查看 | 顯示儲存體帳戶屬性。 |
| az network front-door create | 建立 Front Door。 |
| az network dns record-set | 管理 DNS 記錄和記錄集。 |
| az network front-door | 管理 Front Door。 |
後續步驟
如需 Azure CLI 的詳細資訊,請參閱 Azure CLI 文件。