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

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

显示到 Azure 应用的 Azure 门户导航的屏幕截图。

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

  • 使用 CNAME 记录映射子域(例如 www.contoso.com)。Map a subdomain (for example, www.contoso.com) by using a CNAME record.
  • 使用 A 记录映射根域(例如 contoso.com)。Map a root domain (for example, contoso.com) by using an A record.
  • 使用 CNAME 记录映射通配符域(例如 *.contoso.com)。Map a wildcard domain (for example, *.contoso.com) by using a CNAME record.
  • 将默认 URL 重定向到自定义目录。Redirect the default URL to a custom directory.
  • 使用脚本自动执行域映射。Automate domain mapping with scripts.

先决条件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.

准备应用Prepare the app

若要将自定义 DNS 名称映射到 Web 应用,Web 应用的应用服务计划必须位于付费层(Azure Functions 的“共享”、“基本”、“标准”、“高级”或“消耗”层) 。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, Premium or Consumption for Azure Functions). 在此步骤中,需确保应用服务计划位于受支持的定价层。In this step, you make sure that the App Service app is in the supported pricing tier.

备注

应用服务免费和共享(预览)托管计划是基本层,与其他应用服务应用在相同的 Azure 虚拟机上运行。App Service Free and Shared (preview) hosting plans are base tiers that run on the same Azure virtual machines as other App Service apps. 某些应用可能属于其他客户。Some apps might 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.

在 Azure 门户中选择应用Select the app in the Azure portal

  1. 搜索并选择“应用服务”。Search for and select App Services.

    演示如何选择应用服务的屏幕截图。

  2. 在“应用服务”页上,选择 Azure 应用的名称。On the App Services page, select the name of your Azure app.

    显示到 Azure 应用的门户导航的屏幕截图。

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

检查定价层Check the pricing tier

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

    显示“纵向扩展(应用服务计划)”菜单的屏幕截图。

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

    显示“建议的定价层”的屏幕截图。

  3. 如果应用服务计划不在 F1 层中,请关闭“纵向扩展”页并跳转到映射 CNAME 记录If the App Service plan isn't in the F1 tier, close the Scale up page and skip to Map a CNAME record.

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

  1. 选择任何非免费层(D1B1B2B3,或“生产”类别中的任何层)。Select any of the non-free tiers (D1, B1, B2, B3, or any tier in the Production category). 有关其他选项,请选择“查看其他选项”。For additional options, select See additional options.

  2. 选择“应用”。Select Apply.

    显示检查定价层的屏幕截图。

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

    显示缩放操作确认的屏幕截图。

获取域验证 IDGet a domain verification ID

若要将自定义域添加到应用,需要使用域提供程序添加验证 ID 作为 TXT 记录来验证你对域的所有权。To add a custom domain to your app, you need to verify your ownership of the domain by adding a verification ID as a TXT record with your domain provider. 在应用页的左侧窗格中,选择“自定义域”。In the left pane of your app page, select Custom domains. 复制“自定义域”页中的“自定义域验证 ID”框中的 ID,以便进行下一步操作 。Copy the ID in the Custom Domain Verification ID box in the Custom Domains page for the next step.

显示“自定义域验证 ID”框中的 ID 的屏幕截图。

警告

向自定义域添加域验证 ID 可防止出现无关联的 DNS 条目,并避免子域接管。Adding domain verification IDs to your custom domain can prevent dangling DNS entries and to avoid subdomain takeovers.

映射域Map your domain

可以使用 CNAME 记录或 A 记录将自定义 DNS 名称映射到应用服务。You can use either a CNAME record or an A record to map a custom DNS name to App Service. 请按照相应的步骤操作:Follow the respective steps:

备注

应该对除根域(例如 contoso.com)以外的所有自定义 DNS 名称使用 CNAME 记录。You should use CNAME records for all custom DNS names except root domains (for example, contoso.com). 对于根域,请使用 A 记录。For root domains, use A records.

映射 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).

如果你有除 www 以外的子域,请将 www 替换为你的子域(例如,如果你的自定义域为 sub.constoso.com,则使用 sub 进行替换)。If you have a subdomain other than www, replace www with your subdomain (for example, with sub if your custom domain is sub.constoso.com).

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

备注

可以使用 Azure DNS 为 Azure 应用服务配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for Azure App Service. 有关详细信息,请参阅使用 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

将子域映射到应用的默认域名(<app-name>.chinacloudsites.cn,其中 <app-name> 是应用的名称)。Map a subdomain to the app's default domain name (<app-name>.chinacloudsites.cn, where <app-name> is the name of your app). 若要为 www 子域创建 CNAME 映射,请创建两条记录:To create a CNAME mapping for the www subdomain, create two records:

记录类型Record type 主机Host Value 注释Comments
CNAMECNAME www <app-name>.chinacloudsites.cn 域映射本身。The domain mapping itself.
TXTTXT asuid.www 之前获得的验证 IDThe verification ID you got earlier 应用服务访问 asuid.<subdomain> TXT 记录以验证你对自定义域的所有权。App Service accesses the asuid.<subdomain> TXT record to verify your ownership of the custom domain.

添加 CNAME 和 TXT 记录后,DNS 记录页如下例所示:After you add the CNAME and TXT records, the DNS records page looks like the following example:

显示到 Azure 应用的门户导航的屏幕截图。

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

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

    显示“自定义域”菜单的屏幕截图。

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

  3. 选择“添加自定义域”。Select Add custom domain.

    显示“添加主机名”项的屏幕截图。

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

  5. 选择“验证”。Select Validate. 此时将显示“添加自定义域”页。The Add custom domain page appears.

  6. 确保“主机名记录类型”设置为“CNAME (www.example.com 或任何子域)”。Make sure that Hostname record type is set to CNAME (www.example.com or any subdomain). 选择“添加自定义域”。Select Add custom domain.

    显示“添加自定义域”按钮的屏幕截图。

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

    演示如何添加 CNAME 记录的屏幕截图。

    备注

    自定义域带有警告标签意味着该域尚未绑定到 TLS/SSL 证书。A warning label for your custom domain means that it's not yet bound to a TLS/SSL certificate. 从浏览器向自定义域发出任何 HTTPS 请求都会收到错误或警告,具体取决于浏览器。Any HTTPS request from a browser to your custom domain will receive an error or warning, depending on the browser. 若要添加 TLS 绑定,请参阅在 Azure 应用服务中使用 TLS/SSL 绑定保护自定义 DNS 名称To add a TLS binding, see Secure a custom DNS name with a TLS/SSL binding in Azure App Service.

    如果之前错过了某个步骤或者在某个位置的输入不正确,则会在页面的底部显示验证错误。If you missed a step or made a typo somewhere earlier, a verification error appears 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 on the app's Custom Domains page in the Azure portal.

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

    显示“自定义域”菜单的屏幕截图。

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

    显示到 Azure 应用的门户导航的屏幕截图。

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

备注

可以使用 Azure DNS 为 Azure 应用服务配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for Azure App Service. 有关详细信息,请参阅使用 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 记录映射到应用(通常是根域),请创建两条记录:To map an A record to an app, usually to the root domain, create two records:

记录类型Record type 主机Host Value 注释Comments
AA @ 复制应用的 IP 地址步骤中复制的 IP 地址IP address from Copy the app's IP address 域映射本身(@ 通常表示根域)。The domain mapping itself (@ typically represents the root domain).
TXTTXT asuid 之前获得的验证 IDThe verification ID you got earlier 应用服务访问 asuid.<subdomain> TXT 记录以验证你对自定义域的所有权。App Service accesses the asuid.<subdomain> TXT record to verify your ownership of the custom domain. 对于根域,请使用 asuidFor the root domain, use asuid.

备注

若要使用 A 记录(而不是建议的 CNAME 记录)添加子域(如 www.contoso.com),A 记录和 TXT 记录应如下表所示:To add a subdomain (like www.contoso.com) by using an A record instead of a recommended CNAME record, your A record and TXT record should look like the following table instead:

记录类型Record type 主机Host Value
AA www 复制应用的 IP 地址步骤中复制的 IP 地址IP address from Copy the app's IP address
TXTTXT asuid.www <app-name>.chinacloudsites.cn

添加记录后,DNS 记录页与以下示例相似:After 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.

  1. 选择“添加自定义域”。Select Add custom domain.

    演示如何添加主机名的屏幕截图。

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

  3. 选择“验证”。Select Validate. 此时会显示“添加自定义域”页。The Add custom domain page is shown.

  4. 确保“主机名记录类型”设置为“A 记录 (example.com)”。 Make sure that Hostname record type is set to A record (example.com). 选择“添加自定义域”。Select Add custom domain.

    演示如何将主机名添加到应用的屏幕截图。

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

    演示如何添加 A 记录的屏幕截图。

    备注

    自定义域带有警告标签意味着该域尚未绑定到 TLS/SSL 证书。A warning label for your custom domain means that it's not yet bound to a TLS/SSL certificate. 从浏览器向自定义域发出任何 HTTPS 请求都会收到错误或警告,具体取决于浏览器。Any HTTPS request from a browser to your custom domain will receive an error or warning, depending on the browser. 若要添加 TLS 绑定,请参阅在 Azure 应用服务中使用 TLS/SSL 绑定保护自定义 DNS 名称To add a TLS binding, see Secure a custom DNS name with a TLS/SSL binding in Azure App Service.

    如果之前错过了某个步骤或者在某个位置的输入不正确,则会在页面的底部显示验证错误。If you missed a step or made a typo somewhere earlier, a verification error appears 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 a domain provider

备注

可以使用 Azure DNS 为 Azure 应用服务配置自定义 DNS 名称。You can use Azure DNS to configure a custom DNS name for Azure App Service. 有关详细信息,请参阅使用 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

将通配符名称 * 映射到应用的默认域名(<app-name>.chinacloudsites.cn,其中 <app-name> 是应用的名称)。Map a wildcard name * to the app's default domain name (<app-name>.chinacloudsites.cn, where <app-name> is the name of your app). 若要映射通配符名称,请创建两条记录:To map the wildcard name, create two records:

记录类型Record type 主机Host Value 注释Comments
CNAMECNAME * <app-name>.chinacloudsites.cn 域映射本身。The domain mapping itself.
TXTTXT asuid 之前获得的验证 IDThe verification ID you got earlier 应用服务访问 asuid TXT 记录以验证你对自定义域的所有权。App Service accesses the asuid TXT record to verify your ownership of the custom domain.

*.contoso.com 域示例中, CNAME 记录将把名称 * 映射到 <app-name>.chinacloudsites.cnFor the *.contoso.com domain example, the CNAME record will map the name * to <app-name>.chinacloudsites.cn.

添加 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 都匹配 *.contoso.com)添加到应用中。You can now add any subdomain that matches the wildcard name to the app (for example, sub1.contoso.com, sub2.contoso.com and *.contoso.com both match *.contoso.com).

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

    显示“自定义域”菜单的屏幕截图。

  2. 选择“添加自定义域”。Select Add custom domain.

    演示如何添加主机名的屏幕截图。

  3. 键入与通配符域相匹配的完全限定的域名(例如 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 custom domain button is activated.

  4. 确保“主机名记录类型”设置为“CNAME 记录(www.example.com 或任何子域)”。Make sure that Hostname record type is set to CNAME record (www.example.com or any subdomain). 选择“添加自定义域”。Select Add custom domain.

    显示将 DNS 名称添加到应用的屏幕截图。

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

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

    演示如何添加 CNAME 记录的屏幕截图。

    备注

    自定义域带有警告标签意味着该域尚未绑定到 TLS/SSL 证书。A warning label for your custom domain means that it's not yet bound to a TLS/SSL certificate. 从浏览器向自定义域发出任何 HTTPS 请求都会收到错误或警告,具体取决于浏览器。Any HTTPS request from a browser to your custom domain will receive an error or warning, depending on the browser. 若要添加 TLS 绑定,请参阅在 Azure 应用服务中使用 TLS/SSL 绑定保护自定义 DNS 名称To add a TLS binding, see Secure a custom DNS name with a TLS/SSL binding in Azure App Service.

在浏览器中测试Test in a browser

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

显示到 Azure 应用的导航的屏幕截图。

解决 404“未找到”错误Resolve 404 "Not Found"

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

  • 配置的自定义域缺少 A 记录或 CNAME 记录。The custom domain configured is missing an A record 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.

迁移活动域Migrate an active domain

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

重定向到自定义目录Redirect to a custom directory

默认情况下,应用服务将 Web 请求定向到应用代码的根目录下。By default, App Service directs web requests to the root directory of your app code. 但某些 Web 框架不在根目录下启动。But 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 is accessible at http://contoso.com/public, but you want to direct http://contoso.com to the public directory instead. 此步骤不涉及 DNS 解析,但会涉及到自定义虚拟目录。This step doesn't involve DNS resolution but is about customizing the virtual directory.

若要自定义虚拟目录,请在 Web 应用页的左侧窗格中选择“应用程序设置”。To do customize a virtual directory, select Application settings in the left pane 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)。After the operation finishes, 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 by 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

备注

本文进行了更新,以便使用新的 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.

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

Set-AzWebApp `
    -Name <app-name> `
    -ResourceGroupName <resource_group_name> ` 
    -HostNames @("<fully_qualified_domain_name>","<app-name>.chinacloudsites.cn")

有关详细信息,请参阅将自定义域分配到 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.
  • 将默认 URL 重定向到自定义目录。Redirect the default URL to a custom directory.
  • 使用脚本自动执行域映射。Automate domain mapping with scripts.

继续学习下一教程,了解如何将自定义 TLS/SSL 证书绑定到 Web 应用。Continue to the next tutorial to learn how to bind a custom TLS/SSL certificate to a web app.