您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:将现有的自定义 DNS 名称映射到 Azure Web 应用Tutorial: Map an existing custom DNS name to Azure Web Apps

Azure Web 应用提供高度可缩放、自修补的 Web 托管服务。Azure Web Apps provides a highly scalable, self-patching web hosting service. 本教程介绍如何将现有的自定义 DNS 名称映射到 Azure Web 应用。This tutorial shows you how to map an existing custom DNS name to Azure Web Apps.

在门户中导航到 Azure 应用

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 使用 CNAME 记录映射子域(例如 www.contoso.comMap a subdomain (for example, www.contoso.com) by using a CNAME record
  • 使用 A 记录映射根域(例如 contoso.comMap a root domain (for example, contoso.com) by using an A record
  • 使用 CNAME 记录映射通配符域(例如 *.contoso.comMap a wildcard domain (for example, *.contoso.com) by using a CNAME record
  • 使用脚本自动执行域映射Automate domain mapping with scripts

可以使用 CNAME 记录A 记录将自定义 DNS 名称映射到应用服务。You can use either a CNAME record or an A record to map a custom DNS name to App Service.

备注

我们建议对除根域(例如 contoso.com)以外的所有自定义 DNS 名称使用 CNAME。We recommend that you use a CNAME for all custom DNS names except a root domain (for example, contoso.com).

若要将实时站点及其 DNS 域名迁移到应用服务,请参阅将活动 DNS 名称迁移到 Azure 应用服务To migrate a live site and its DNS domain name to App Service, see Migrate an active DNS name to Azure App Service.

先决条件Prerequisites

完成本教程:To complete this tutorial:

  • 创建应用服务应用,或使用为另一教程创建的应用。Create an App Service app, or use an app that you created for another tutorial.
  • 购买域名,确保可以访问域提供商(如 GoDaddy)的 DNS 注册表。Purchase a domain name and make sure you have access to the DNS registry for your domain provider (such as GoDaddy).

    例如,若要添加 contoso.comwww.contoso.com 的 DNS 条目,必须能够配置 contoso.com 根域的 DNS 设置。For example, to add DNS entries for contoso.com and www.contoso.com, you must be able to configure the DNS settings for the contoso.com root domain.

    备注

    如果现有还没有域名,请考虑使用 Azure 门户购买域If you don't have an existing domain name, consider purchasing a domain using the Azure portal.

准备应用Prepare the app

若要映射自定义 DNS 名称到 Web 应用,Web 应用的应用服务计划必须位于付费层(“共享”、“基本”、“标准”或“高级”)。To map a custom DNS name to a web app, the web app's App Service plan must be a paid tier (Shared, Basic, Standard, or Premium). 在此步骤中,需确保应用服务计划位于受支持的定价层。In this step, you make sure that the App Service app is in the supported pricing tier.

备注

应用服务免费和共享(预览)托管计划是基本层,与其他应用服务应用在相同的 Azure VM 上运行。App Service Free and Shared (preview) hosting plans are base tiers that run on the same Azure VM as other App Service apps. 某些应用可能属于其他客户。Some apps may belong to other customers. 这些层仅旨在用于开发和测试目的。These tiers are intended to be used only for development and testing purposes.

登录 AzureSign in to Azure

打开 Azure 门户,然后使用 Azure 帐户登录。Open the Azure portal and sign in with your Azure account.

从左侧菜单中选择“应用服务”,然后选择应用名称。From the left menu, select App Services, and then select the name of the app.

在门户中导航到 Azure 应用

将看到应用服务应用的管理页。You see the management page of the App Service app.

检查定价层Check the pricing tier

在应用页的左侧导航窗格中,向下滚动到“设置”部分,然后选择“增加(应用服务计划)”。In the left navigation of the app page, scroll to the Settings section and select Scale up (App Service plan).

扩展菜单

蓝色边框突出显示了应用的当前层。The app's current tier is highlighted by a blue border. 检查以确保应用不在“免费”层中。Check to make sure that the app is not in the Free tier. 免费层不支持自定义 DNS。Custom DNS is not supported in the Free tier.

检查定价层

如果应用服务计划不免费,请关闭选择定价层页面并跳转到映射 CNAME记录If the App Service plan is not Free, close the Choose your pricing tier page and skip to Map a CNAME record.

增加应用服务计划Scale up the App Service plan

选择任一非免费层(“共享”、“基本”、“标准”或“高级”)。Select any of the non-free tiers (Shared, Basic, Standard, or Premium).

单击“选择”。Click Select.

检查定价层

看到以下通知时,说明缩放操作已完成。When you see the following notification, the scale operation is complete.

缩放操作确认

映射 CNAME 记录Map a CNAME record

在教程示例中,为 www 子域(例如 www.contoso.com)添加 CNAME 记录。In the tutorial example, you add a CNAME record for the www subdomain (for example, www.contoso.com).

通过域提供商访问 DNS 记录Access DNS records with domain provider

备注

可以使用 Azure DNS 为 Azure Web 应用配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for your Azure Web Apps. 有关详细信息,请参阅使用 Azure DNS 为 Azure 服务提供自定义域设置For more information, see Use Azure DNS to provide custom domain settings for an Azure service.

请登录到域提供商的网站。Sign in to the website of your domain provider.

查找管理 DNS 记录的页面。Find the page for managing DNS records. 每个域提供商都有自己的 DNS 记录界面,因此请查阅提供商的文档。Every domain provider has its own DNS records interface, so consult the provider's documentation. 查找站点中标记为“域名”、“DNS”或“名称服务器管理”的区域。Look for areas of the site labeled Domain Name, DNS, or Name Server Management.

通常通过查看帐户信息,然后查找如“我的域”之类的链接,便可以找到 DNS 记录页面。Often, you can find the DNS records page by viewing your account information, and then looking for a link such as My domains. 转到该页面,然后查找名称类似于“区域文件”、“DNS 记录”或“高级配置”的链接。Go to that page and then look for a link that is named something like Zone file, DNS Records, or Advanced configuration.

以下屏幕截图是 DNS 记录页的一个示例:The following screenshot is an example of a DNS records page:

示例 DNS 记录页

在示例屏幕截图中,选择“添加”以创建记录。In the example screenshot, you select Add to create a record. 某些提供商提供了不同的链接来添加不同的记录类型。Some providers have different links to add different record types. 同样,请查阅提供商的文档。Again, consult the provider's documentation.

备注

对于某些提供商(例如 GoDaddy),在你选择单独的“保存更改”链接之前,这些 DNS 记录不会生效。For certain providers, such as GoDaddy, changes to DNS records don't become effective until you select a separate Save Changes link.

创建 CNAME 记录Create the CNAME record

添加一条 CNAME 记录,以便将子域映射到应用的默认主机名(<app_name>.azurewebsites.net,其中 <app_name> 是应用的名称)。Add a CNAME record to map a subdomain to the app's default hostname (<app_name>.azurewebsites.net, where <app_name> is the name of your app).

www.contoso.com 域示例中,添加将名称 www 映射到 <app_name>.azurewebsites.net 的 CNAME 记录。For the www.contoso.com domain example, add a CNAME record that maps the name www to <app_name>.azurewebsites.net.

添加 CNAME 后,DNS 记录页与以下示例相似:After you add the CNAME, the DNS records page looks like the following example:

在门户中导航到 Azure 应用

在 Azure 中启用 CNAME 记录映射Enable the CNAME record mapping in Azure

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

自定义域菜单

在应用的“自定义域”页中,将完全限定的自定义 DNS 名称添加到 (www.contoso.com) 列表。In the Custom domains page of the app, add the fully qualified custom DNS name (www.contoso.com) to the list.

选择“添加主机名”旁边的 + 图标。Select the + icon next to Add hostname.

添加主机名

键入已添加 CNAME 记录的完全限定的域名,如 www.contoso.comType the fully qualified domain name that you added a CNAME record for, such as www.contoso.com.

选择“验证”。Select Validate.

“添加主机名”按钮会被激活。The Add hostname button is activated.

确保“主机名记录类型”设置为“CNAME (www.example.com 或任何子域)”。Make sure that Hostname record type is set to CNAME (www.example.com or any subdomain).

选择“添加主机名”。Select Add hostname.

将 DNS 名称添加到应用

新主机名可能需要经过一段时间后才会反映在应用的“自定义域”页面中。It might take some time for the new hostname to be reflected in the app's Custom domains page. 请尝试刷新浏览器来更新数据。Try refreshing the browser to update the data.

已添加 CNAME 记录

如果之前错过了某个步骤或者在某个位置的输入不正确,则会在页面的底部看到验证错误。If you missed a step or made a typo somewhere earlier, you see a verification error at the bottom of the page.

验证错误

映射 A 记录Map an A record

在教程示例中,为根域(例如 contoso.com)添加 A 记录。In the tutorial example, you add an A record for the root domain (for example, contoso.com).

复制应用的 IP 地址Copy the app's IP address

若要映射 A 记录,需要应用的外部 IP 地址。To map an A record, you need the app's external IP address. 可以在 Azure 门户中应用的“自定义域”页中找到此 IP 地址。You can find this IP address in the app's Custom domains page in the Azure portal.

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

自定义域菜单

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

在门户中导航到 Azure 应用

通过域提供商访问 DNS 记录Access DNS records with domain provider

备注

可以使用 Azure DNS 为 Azure Web 应用配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for your Azure Web Apps. 有关详细信息,请参阅使用 Azure DNS 为 Azure 服务提供自定义域设置For more information, see Use Azure DNS to provide custom domain settings for an Azure service.

请登录到域提供商的网站。Sign in to the website of your domain provider.

查找管理 DNS 记录的页面。Find the page for managing DNS records. 每个域提供商都有自己的 DNS 记录界面,因此请查阅提供商的文档。Every domain provider has its own DNS records interface, so consult the provider's documentation. 查找站点中标记为“域名”、“DNS”或“名称服务器管理”的区域。Look for areas of the site labeled Domain Name, DNS, or Name Server Management.

通常通过查看帐户信息,然后查找如“我的域”之类的链接,便可以找到 DNS 记录页面。Often, you can find the DNS records page by viewing your account information, and then looking for a link such as My domains. 转到该页面,然后查找名称类似于“区域文件”、“DNS 记录”或“高级配置”的链接。Go to that page and then look for a link that is named something like Zone file, DNS Records, or Advanced configuration.

以下屏幕截图是 DNS 记录页的一个示例:The following screenshot is an example of a DNS records page:

示例 DNS 记录页

在示例屏幕截图中,选择“添加”以创建记录。In the example screenshot, you select Add to create a record. 某些提供商提供了不同的链接来添加不同的记录类型。Some providers have different links to add different record types. 同样,请查阅提供商的文档。Again, consult the provider's documentation.

备注

对于某些提供商(例如 GoDaddy),在你选择单独的“保存更改”链接之前,这些 DNS 记录不会生效。For certain providers, such as GoDaddy, changes to DNS records don't become effective until you select a separate Save Changes link.

创建 A 记录Create the A record

若要将 A 记录映射到应用,应用服务需要两个DNS 记录:To map an A record to an app, App Service requires two DNS records:

  • 要映射到应用 IP 地址的 A 记录。An A record to map to the app's IP address.
  • 要映射到应用默认主机名 <app_name>.azurewebsites.net 的 TXT 记录。A TXT record to map to the app's default hostname <app_name>.azurewebsites.net. 应用服务仅在配置时使用此记录,以验证你是否拥有自定义域。App Service uses this record only at configuration time, to verify that you own the custom domain. 自定义域经过验证并且在应用服务中配置后,可以删除此 TXT 记录。After your custom domain is validated and configured in App Service, you can delete this TXT record.

contoso.com 域示例根据下表创建 A 和 TXT 记录(@ 通常表示根域)。For the contoso.com domain example, create the A and TXT records according to the following table (@ typically represents the root domain).

记录类型Record type 主机Host Value
AA @ 复制应用的 IP 地址步骤中复制的 IP 地址IP address from Copy the app's IP address
TXTTXT @ <app_name>.azurewebsites.net

添加记录后,DNS 记录页与以下示例相似:When the records are added, the DNS records page looks like the following example:

DNS 记录页

在应用中启用 A 记录映射Enable the A record mapping in the app

在 Azure 门户中返回到应用的“自定义域”页。将完全限定的自定义 DNS 名称(例如 contoso.com)添加到列表。Back in the app's Custom domains page in the Azure portal, add the fully qualified custom DNS name (for example, contoso.com) to the list.

选择“添加主机名”旁边的 + 图标。Select the + icon next to Add hostname.

添加主机名

键入已配置 A 记录的完全限定的域名,如 contoso.comType the fully qualified domain name that you configured the A record for, such as contoso.com.

选择“验证”。Select Validate.

“添加主机名”按钮会被激活。The Add hostname button is activated.

确保“主机名记录类型”设置为“A 记录 (example.com)”。Make sure that Hostname record type is set to A record (example.com).

选择“添加主机名”。Select Add hostname.

将 DNS 名称添加到应用

新主机名可能需要经过一段时间后才会反映在应用的“自定义域”页面中。It might take some time for the new hostname to be reflected in the app's Custom domains page. 请尝试刷新浏览器来更新数据。Try refreshing the browser to update the data.

已添加 A 记录

如果之前错过了某个步骤或者在某个位置的输入不正确,则会在页面的底部看到验证错误。If you missed a step or made a typo somewhere earlier, you see a verification error at the bottom of the page.

验证错误

映射通配符域Map a wildcard domain

在教程示例中,你通过添加 CNAME 记录将通配符 DNS 名称(例如 *.contoso.com)映射到应用服务应用。In the tutorial example, you map a wildcard DNS name (for example, *.contoso.com) to the App Service app by adding a CNAME record.

通过域提供商访问 DNS 记录Access DNS records with domain provider

备注

可以使用 Azure DNS 为 Azure Web 应用配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for your Azure Web Apps. 有关详细信息,请参阅使用 Azure DNS 为 Azure 服务提供自定义域设置For more information, see Use Azure DNS to provide custom domain settings for an Azure service.

请登录到域提供商的网站。Sign in to the website of your domain provider.

查找管理 DNS 记录的页面。Find the page for managing DNS records. 每个域提供商都有自己的 DNS 记录界面,因此请查阅提供商的文档。Every domain provider has its own DNS records interface, so consult the provider's documentation. 查找站点中标记为“域名”、“DNS”或“名称服务器管理”的区域。Look for areas of the site labeled Domain Name, DNS, or Name Server Management.

通常通过查看帐户信息,然后查找如“我的域”之类的链接,便可以找到 DNS 记录页面。Often, you can find the DNS records page by viewing your account information, and then looking for a link such as My domains. 转到该页面,然后查找名称类似于“区域文件”、“DNS 记录”或“高级配置”的链接。Go to that page and then look for a link that is named something like Zone file, DNS Records, or Advanced configuration.

以下屏幕截图是 DNS 记录页的一个示例:The following screenshot is an example of a DNS records page:

示例 DNS 记录页

在示例屏幕截图中,选择“添加”以创建记录。In the example screenshot, you select Add to create a record. 某些提供商提供了不同的链接来添加不同的记录类型。Some providers have different links to add different record types. 同样,请查阅提供商的文档。Again, consult the provider's documentation.

备注

对于某些提供商(例如 GoDaddy),在你选择单独的“保存更改”链接之前,这些 DNS 记录不会生效。For certain providers, such as GoDaddy, changes to DNS records don't become effective until you select a separate Save Changes link.

创建 CNAME 记录Create the CNAME record

添加一条 CNAME 记录,以便将通配符域名称名映射到应用的默认主机名 (<app_name>.azurewebsites.net)。Add a CNAME record to map a wildcard name to the app's default hostname (<app_name>.azurewebsites.net).

*.contoso.com 域示例中, CNAME 记录将把名称 * 映射到 <app_name>.azurewebsites.netFor the *.contoso.com domain example, the CNAME record will map the name * to <app_name>.azurewebsites.net.

添加 CNAME 时,DNS 记录页与以下示例相似:When the CNAME is added, the DNS records page looks like the following example:

在门户中导航到 Azure 应用

在应用中启用 CNAME 记录映射Enable the CNAME record mapping in the app

现在,可以将任何与通配符名称匹配的子域添加到应用中(例如 sub1.contoso.comsub2.contoso.com 匹配 *.contoso.com)。You can now add any subdomain that matches the wildcard name to the app (for example, sub1.contoso.com and sub2.contoso.com match *.contoso.com).

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

自定义域菜单

选择“添加主机名”旁边的 + 图标。Select the + icon next to Add hostname.

添加主机名

键入与通配符域相匹配的完全限定的域名(例如 sub1.contoso.com),然后选择“验证”。Type a fully qualified domain name that matches the wildcard domain (for example, sub1.contoso.com), and then select Validate.

“添加主机名”按钮会被激活。The Add hostname button is activated.

确保“主机名记录类型”设置为“CNAME 记录 (www.example.com 或任何子域)”。Make sure that Hostname record type is set to CNAME record (www.example.com or any subdomain).

选择“添加主机名”。Select Add hostname.

将 DNS 名称添加到应用

新主机名可能需要经过一段时间后才会反映在应用的“自定义域”页面中。It might take some time for the new hostname to be reflected in the app's Custom domains page. 请尝试刷新浏览器来更新数据。Try refreshing the browser to update the data.

再次选择 + 图标,添加另一个与通配符域匹配的主机名。Select the + icon again to add another hostname that matches the wildcard domain. 例如,添加 sub2.contoso.comFor example, add sub2.contoso.com.

已添加 CNAME 记录

在浏览器中测试Test in browser

浏览至你之前配置的 DNS 名称(例如,contoso.comwww.contoso.comsub1.contoso.comsub2.contoso.com)。Browse to the DNS name(s) that you configured earlier (for example, contoso.com, www.contoso.com, sub1.contoso.com, and sub2.contoso.com).

在门户中导航到 Azure 应用

解决 404 错误“未找到网站”Resolve 404 error “Web Site not found”

如果在浏览到自定义域的 URL 时收到 HTTP 404(未找到)错误,请验证域是否使用 WhatsmyDNS.net 对应用的 IP 地址进行解析。If you receive an HTTP 404 (Not Found) error when browsing to the URL of your custom domain, verify that your domain resolves to your app's IP address using WhatsmyDNS.net. 如果没有,则可能是以下原因之一造成的:If not, it may be due to one of the following reasons:

  • 配置的自定义域缺少 A 记录和/或 CNAME 记录。The custom domain configured is missing an A record and/or a CNAME record.
  • 浏览器客户端已缓存域的旧 IP 地址。The browser client has cached the old IP address of your domain. 再次清除缓存并测试 DNS 解析。Clear the cache and test DNS resolution again. 在 Windows 计算机上,使用 ipconfig /flushdns 清除缓存。On a Windows machine, you clear the cache with ipconfig /flushdns.

将默认 URL 定向到自定义目录Direct default URL to a custom directory

默认情况下,应用服务将 Web 请求定向到应用代码的根目录下。By default, App Service directs web requests to the root directory of your app code. 但是,某些 Web 框架不在根目录下启动。However, certain web frameworks don't start in the root directory. 例如,Laravelpublic 子目录中启动。For example, Laravel starts in the public subdirectory. 若要继续 contoso.com DNS 示例,此类应用应可在 http://contoso.com/public 中访问,但你实际上想要将 http://contoso.com 直接定向到 public 目录。To continue the contoso.com DNS example, such an app would be accessible at http://contoso.com/public, but you would really want to direct http://contoso.com to the public directory instead. 此步骤不涉及 DNS 解析,但涉及到自定义虚拟目录。This step doesn't involve DNS resolution, but customizing the virtual directory.

若要执行此操作,请选择 Web 应用页左侧导航窗格中的“应用程序设置”。To do this, select Application settings in the left-hand navigation of your web app page.

在页面底部,根虚拟目录 / 默认指向 site\wwwroot,这是应用代码的根目录。At the bottom of the page, the root virtual directory / points to site\wwwroot by default, which is the root directory of your app code. 将其改为指向例如 site\wwwroot\public,并保存所做的更改。Change it to point to the site\wwwroot\public instead, for example, and save your changes.

自定义虚拟目录

操作完成后,应用会返回根路径的正确页面(例如,http://contoso.com)。Once the operation completes, your app should return the right page at the root path (for example, http://contoso.com).

使用脚本自动化Automate with scripts

可以在 Azure CLIAzure PowerShell 中使用脚本自动管理自定义域。You can automate management of custom domains with scripts, using the Azure CLI or Azure PowerShell.

Azure CLIAzure CLI

以下命令将配置的自定义 DNS 名称添加到应用服务应用。The following command adds a configured custom DNS name to an App Service app.

az webapp config hostname add \
    --webapp-name <app_name> \
    --resource-group <resource_group_name> \ 
    --hostname <fully_qualified_domain_name> 

有关详细信息,请参阅将自定义域映射到 Web 应用For more information, see Map a custom domain to a web app.

Azure PowerShellAzure PowerShell

以下命令将配置的自定义 DNS 名称添加到应用服务应用。The following command adds a configured custom DNS name to an App Service app.

Set-AzureRmWebApp `
    -Name <app_name> `
    -ResourceGroupName <resource_group_name> ` 
    -HostNames @("<fully_qualified_domain_name>","<app_name>.azurewebsites.net") 

有关详细信息,请参阅将自定义域分配到 Web 应用For more information, see Assign a custom domain to a web app.

后续步骤Next steps

本教程介绍了如何:In this tutorial, you learned how to:

  • 使用 CNAME 记录映射子域Map a subdomain by using a CNAME record
  • 使用 A 记录映射根域Map a root domain by using an A record
  • 使用 CNAME 记录映射通配符域Map a wildcard domain by using a CNAME record
  • 使用脚本自动执行域映射Automate domain mapping with scripts

转到下一教程,了解如何将自定义 SSL 证书绑定到 Web 应用。Advance to the next tutorial to learn how to bind a custom SSL certificate to a web app.