Azure Front Door:部署自訂網域

適用於: ✔️ Front Door (傳統)

Important

Azure Front Door(經典版)將於 2027 年 3 月 31 日退休。 由於該服務即將退休,不再支援設定檔建立、新網域導入或管理憑證。 為避免服務中斷,遷移至Azure Front Door標準版或高級版。 如需詳細資訊,請參閱 ⁠Azure Front Door (傳統版) 淘汰。

此 Azure CLI 指令碼範例示範如何在 Azure Front Door 前端上部署自訂網域名稱與 TLS 憑證。 此指令碼會使用自訂網域名稱 (由 Azure DNS 裝載) 與 TLS 憑證,自動佈建 Azure Front Door。

Important

請確定您的網域名稱名稱已有 Azure DNS 公用區域存在。 如需教學課程,請參閱在 Azure DNS 中裝載您的網域

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

Prerequisites

範例腳本

啟動 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'

如需詳細資訊,請參閱設定使用中訂用帳戶 (部分機器翻譯) 或以互動方式登入 (部分機器翻譯)。

入門指南

指令碼:

  1. 建立資源群組。
  2. 建立儲存帳號以承載單頁應用程式(SPA)。
  3. 啟用儲存體帳戶上的 SPA 裝載。
  4. 上傳「Hello world!」檔案。 index.html
  5. 建立 Front Door 設定檔。
  6. 建立解析至 Front Door 的 Apex DNS 別名。
  7. 為 adverify 主機名稱建立一個 CNAME。
  8. 為自訂網域建立一個前門前端端點。
  9. 新增從自訂網域前端到 SPA 來源的路由。
  10. 新增一個路由規則,將 HTTP 重新導向到 HTTPS。
  11. 啟用 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 儲存 blob 上傳 將 Blob 上傳至容器。
az 儲存帳戶查看 顯示儲存體帳戶屬性。
az network front-door create (建立 Azure Front Door) 建立 Front Door。
az network dns record-set (這是一個用於管理 DNS 記錄集合的命令) 管理 DNS 記錄和記錄集。
AZ 網路前門 管理 Front Door。