教程:为 Web 应用在自定义域中创建 DNS 记录Tutorial: Create DNS records in a custom domain for a web app

可以配置 Azure DNS 来托管 Web 应用的自定义域。You can configure Azure DNS to host a custom domain for your web apps. 例如,可创建 Azure Web 应用,并让用户使用 www.contoso.com 或 contoso.com 作为完全限定的域名 (FQDN) 访问它。For example, you can create an Azure web app and have your users access it using either www.contoso.com or contoso.com as a fully qualified domain name (FQDN).


本教程通篇都使用 Contoso.com 作为示例。Contoso.com is used as an example throughout this tutorial. 请将 contoso.com 替换为你自己的域名。Substitute your own domain name for contoso.com.

为此,必须创建三条记录:To do this, you have to create three records:

  • 指向 contoso.com 的根 A 记录A root "A" record pointing to contoso.com
  • 用于验证的根 TXT 记录A root "TXT" record for verification
  • 指向 A 记录的 www 名称的 CNAME 记录A "CNAME" record for the www name that points to the A record

请记住,如果在 Azure 中为 Web 应用创建 A 记录,当 Web 应用的基础 IP 地址更改时,则必须手动更新 A 记录。Keep in mind that if you create an A record for a web app in Azure, the A record must be manually updated if the underlying IP address for the web app changes.

在本教程中,你将了解如何执行以下操作:In this tutorial, you learn how to:

  • 为自定义域创建 A 记录和 TXT 记录Create an A and TXT record for your custom domain
  • 创建自定义域的 CNAME 记录Create a CNAME record for your custom domain
  • 测试新记录Test the new records
  • 为 Web 应用添加自定义主机名Add custom host names you your web app
  • 测试自定义主机名Test the custom host names

如果没有 Azure 订阅,可在开始前创建一个 试用帐户If you don't have an Azure subscription, create a trial account before you begin.



本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

  • 必须有一个可以用来测试的域名,可以在 Azure DNS 中托管该域名。You must have a domain name available to test with that you can host in Azure DNS . 必须能够完全控制此域。You must have full control of this domain. 完全控制包括能够为域设置名称服务器 (NS) 记录。Full control includes the ability to set the name server (NS) records for the domain.

  • 创建应用服务应用,或使用为另一教程创建的应用。Create an App Service app, or use an app that you created for another tutorial.

  • 在 Azure DNS 中创建一个 DNS 区域,并将注册机构中的区域委派给 Azure DNS。Create a DNS zone in Azure DNS, and delegate the zone in your registrar to Azure DNS.

    1. 若要创建 DNS 区域,请按照创建 DNS 区域中的步骤执行操作。To create a DNS zone, follow the steps in Create a DNS zone.
    2. 若要将区域委派给 Azure DNS,请按照 DNS 域委派中的步骤执行操作。To delegate your zone to Azure DNS, follow the steps in DNS domain delegation.

在创建区域并将它委派给 Azure DNS 之后,可以为自定义域创建记录。After creating a zone and delegating it to Azure DNS, you can then create records for your custom domain.

创建一个 A 记录和 TXT 记录Create an A record and TXT record

A 记录可用于将名称映射到其 IP 地址。An A record is used to map a name to its IP address. 在下面的示例中,使用你的 Web 应用 IPv4 地址将“@”分配为 A 记录。In the following example, assign "@" as an A record using your web app IPv4 address. @ 通常表示根域。@ typically represents the root domain.

获取 IPv4 地址Get the IPv4 address

在 Azure 门户中,在应用服务页的左侧导航窗格中,选择“自定义域” 。In the left navigation of the App Services page in the Azure portal, select Custom domains.


在“自定义域” 页中,复制应用的 IPv4 地址:In the Custom domains page, copy the app's IPv4 address:

在门户中导航到 Azure 应用

创建 A 记录Create the A record

New-AzDnsRecordSet -Name "@" -RecordType "A" -ZoneName "contoso.com" `
 -ResourceGroupName "MyAzureResourceGroup" -Ttl 600 `
 -DnsRecords (New-AzDnsRecordConfig -IPv4Address "<your web app IP address>")

创建 TXT 记录Create the TXT record

应用服务仅在配置时使用此记录来验证你是否拥有自定义域。App Services uses this record only at configuration time to verify that you own the custom domain. 自定义域经过验证并且在应用服务中配置后,可以删除此 TXT 记录。You can delete this TXT record after your custom domain is validated and configured in App Service.


如果想要验证域名,但是不将生产流量路由到 Web 应用,只需指定验证步骤的 TXT 记录即可。If you want to verify the domain name, but not route production traffic to the web app, you only need to specify the TXT record for the verification step. 验证不需要除 TXT 记录之外的 A 或 CNAME 记录。Verification does not require an A or CNAME record in addition to the TXT record.

New-AzDnsRecordSet -ZoneName contoso.com -ResourceGroupName MyAzureResourceGroup `
 -Name "@" -RecordType "txt" -Ttl 600 `
 -DnsRecords (New-AzDnsRecordConfig -Value  "contoso.chinacloudsites.cn")

创建 CNAME 记录Create the CNAME record

如果域已由 Azure DNS 管理(请参阅 DNS 域委派),可以使用以下示例创建 contoso.chinacloudsites.cn 的 CNAME 记录。If your domain is already managed by Azure DNS (see DNS domain delegation, you can use the following example to create a CNAME record for contoso.chinacloudsites.cn.

打开 Azure PowerShell 并创建一条新的 CNAME 记录。Open Azure PowerShell and create a new CNAME record. 此示例使用 Web 应用 contoso.chinacloudsites.cn 的别名在名为“contoso.com”的 DNS 区域中创建一个“生存时间”为 600 秒的记录集类型 CNAME。This example creates a record set type CNAME with a "time to live" of 600 seconds in DNS zone named "contoso.com" with the alias for the web app contoso.chinacloudsites.cn.

创建记录Create the record

New-AzDnsRecordSet -ZoneName contoso.com -ResourceGroupName "MyAzureResourceGroup" `
 -Name "www" -RecordType "CNAME" -Ttl 600 `
 -DnsRecords (New-AzDnsRecordConfig -cname "contoso.chinacloudsites.cn")

以下示例是响应:The following example is the response:

    Name              : www
    ZoneName          : contoso.com
    ResourceGroupName : myresourcegroup
    Ttl               : 600
    Etag              : 8baceeb9-4c2c-4608-a22c-229923ee185
    RecordType        : CNAME
    Records           : {contoso.chinacloudsites.cn}
    Tags              : {}

测试新记录Test the new records

可以通过使用 nslookup 查询“www.contoso.com”和“contoso.com”来验证记录是否已正确创建,如下所示:You can validate the records were created correctly by querying the "www.contoso.com" and "contoso.com" using nslookup, as shown below:

PS C:\> nslookup
Default Server:  Default

> www.contoso.com
Server:  default server

Non-authoritative answer:
Name:    <instance of web app service>.chinacloudapp.cn
Address:  <ip of web app service>
Aliases:  www.contoso.com
<instance of web app service>.chinacloudsites.cn

> contoso.com
Server:  default server

Non-authoritative answer:
Name:    contoso.com
Address:  <ip of web app service>

> set type=txt
> contoso.com

Server:  default server

Non-authoritative answer:
contoso.com text =


添加自定义主机名Add custom host names

现在,可以向 Web 应用添加自定义主机名:Now you can add the custom host names to your web app:

set-AzWebApp `
 -Name contoso `
 -ResourceGroupName MyAzureResourceGroup `
 -HostNames @("contoso.com","www.contoso.com","contoso.chinacloudsites.cn")

测试自定义主机名Test the custom host names

打开浏览器并浏览到 http://www.<your domainname>http://<you domain name>Open a browser and browse to http://www.<your domainname> and http://<you domain name>.


请务必包括 http:// 前缀,否则浏览器可能会尝试替你预测 URL!Make sure you include the http:// prefix, otherwise your browser may attempt to predict a URL for you!

对于上述两个 URL,应当会看到相同的页面。You should see the same page for both URLs. 例如:For example:

Contoso 应用服务

清理资源Clean up resources

当不再需要本教程中创建的资源时,可以删除 myresourcegroup 资源组。When you no longer need the resources created in this tutorial, you can delete the myresourcegroup resource group.

后续步骤Next steps

了解如何创建 Azure DNS 专用区域。Learn how to create Azure DNS private zones.