为 Azure 云服务配置自定义域名Configuring a custom domain name for an Azure cloud service

创建云服务时,Azure 会将其分配给 chinacloudapp.cn 的子域。When you create a Cloud Service, Azure assigns it to a subdomain of chinacloudapp.cn. 例如,如果云服务名为“contoso”,则用户能够在 URL(如 http://contoso.chinacloudapp.cn)上访问应用程序。For example, if your Cloud Service is named "contoso", your users will be able to access your application on a URL like http://contoso.chinacloudapp.cn. Azure 还会分配一个虚拟 IP 地址。Azure also assigns a virtual IP address.

但是,还可以在自己的域名(例如 contoso.com)上公开应用程序。However, you can also expose your application on your own domain name, such as contoso.com. 此文章介绍了如何保留或配置云服务 Web 角色的自定义域名称。This article explains how to reserve or configure a custom domain name for Cloud Service web roles.

是否已经了解什么是 CNAME 和 A 记录?Do you already understand what CNAME and A records are? 跳过解释Jump past the explanation.

Note

本任务中的过程适用于 Azure 云服务。The procedures in this task apply to Azure Cloud Services. 有关应用服务,请参阅将现有的自定义 DNS 名称映射到 Azure Web 应用For App Services, see Map an existing custom DNS name to Azure Web Apps. 有关存储帐户,请参阅 为 Azure Blob 存储终结点配置自定义域名For storage accounts, see Configure a custom domain name for your Azure Blob storage endpoint.

Tip

快速入门 - 使用全新的 Azure 操作实例指南Get going faster--use the NEW Azure guided walkthrough! 通过该快速入门,可轻松将自定义域名与 Azure 云服务或 Azure 网站关联起来并确保通信安全 (TLS)。It makes associating a custom domain name AND securing communication (TLS) with Azure Cloud Services or Azure Websites a snap.

了解 CNAME 和 A 记录Understand CNAME and A records

CNAME(即别名记录)和 A 记录都允许将域名与特定服务器(在此示例中为服务)进行关联,但是其工作原理不同。CNAME (or alias records) and A records both allow you to associate a domain name with a specific server (or service in this case,) however they work differently. 对 Azure 云服务使用 A 记录时,在确定使用哪个 A 记录之前,还有一些应该考虑的特定注意事项:There are also some specific considerations when using A records with Azure Cloud services that you should consider before deciding which to use.

CNAME 或别名记录CNAME or Alias record

CNAME 记录会将特定 域(例如 contoso.comwww.contoso.com)映射到规范域名。A CNAME record maps a specific domain, such as contoso.com or www.contoso.com, to a canonical domain name. 在这种情况下,规范域名是 Azure 托管应用程序的 [myapp].chinacloudapp.cn 域名 。In this case, the canonical domain name is the [myapp].chinacloudapp.cn domain name of your Azure hosted application. 创建完成后,CNAME 将为 [myapp].chinacloudapp.cn 创建一个别名 。Once created, the CNAME creates an alias for the [myapp].chinacloudapp.cn. CNAME 条目将自动解析为 [myapp].chinacloudapp.cn 服务的 IP 地址,因此,如果该云服务的 IP 地址发生更改,则无需执行任何操作 。The CNAME entry will resolve to the IP address of your [myapp].chinacloudapp.cn service automatically, so if the IP address of the cloud service changes, you do not have to take any action.

Note

某些域注册机构只允许在使用 CNAME 记录(例如 www.contoso.com)和非根名称(例如 contoso.com)时映射子域。Some domain registrars only allow you to map subdomains when using a CNAME record, such as www.contoso.com, and not root names, such as contoso.com. 有关 CNAME 记录的详细信息,请参阅注册机构提供的文档、CNAME 记录上的 Wikipedia 条目IETF 域名 - 实现和规范文档。For more information on CNAME records, see the documentation provided by your registrar, the Wikipedia entry on CNAME record, or the IETF Domain Names - Implementation and Specification document.

A 记录A record

A 记录将域(例如 contoso.comwww.contoso.com)或通配符域(例如 *.contoso.com)映射到 IP 地址 。An A record maps a domain, such as contoso.com or www.contoso.com, or a wildcard domain such as *.contoso.com, to an IP address. 对于 Azure 云服务案例,则映射到该服务的虚拟 IP。In the case of an Azure Cloud Service, the virtual IP of the service. 因此,与 CNAME 记录相比,A 记录的主要优势是你可持有使用通配符的一个条目,例如 * .contoso.com,用于处理多个子域(例如 mail.contoso.comlogin.contoso.comwww.contso.com)的请求。So the main benefit of an A record over a CNAME record is that you can have one entry that uses a wildcard, such as *.contoso.com, which would handle requests for multiple sub-domains such as mail.contoso.com, login.contoso.com, or www.contso.com.

Note

由于 A 记录映射到静态 IP 地址,它无法自动解析云服务 IP 地址的更改。Since an A record is mapped to a static IP address, it cannot automatically resolve changes to the IP address of your Cloud Service. 第一次部署到空槽位(生产槽位或暂存槽位)时,将分配云服务所用的 IP 地址。如果删除针对该槽的部署,则 Azure 将释放该 IP 地址,将来任何一次部署到该槽都可能给定新的 IP 地址。The IP address used by your Cloud Service is allocated the first time you deploy to an empty slot (either production or staging.) If you delete the deployment for the slot, the IP address is released by Azure and any future deployments to the slot may be given a new IP address.

为方便起见,在暂存和生产部署之间切换或对现有部署执行就地升级时,将保留给定部署槽位(生产或暂存)的 IP 地址。Conveniently, the IP address of a given deployment slot (production or staging) is persisted when swapping between staging and production deployments or performing an in-place upgrade of an existing deployment. 有关执行这些操作的详细信息,请参阅如何管理云服务For more information on performing these actions, see How to manage cloud services.

为自定义域添加 CNAME 记录Add a CNAME record for your custom domain

若要创建 CNAME 记录,必须使用注册机构提供的工具在 DNS 表中为的自定义域添加新条目。To create a CNAME record, you must add a new entry in the DNS table for your custom domain by using the tools provided by your registrar. 每个注册机构指定 CNAME 记录的方法类似但略有不同,但概念是相同的。Each registrar has a similar but slightly different method of specifying a CNAME record, but the concepts are the same.

  1. 使用下列方法之一找到分配给云服务的 .chinacloudapp.cn 域名。Use one of these methods to find the .chinacloudapp.cn domain name assigned to your cloud service.

    • 登录到 Azure 门户,选择云服务,查看“概述”部分,然后找到“站点 URL”条目 。Login to the Azure portal, select your cloud service, look at the Overview section and then find the Site URL entry.

      显示站点 URL 的速览部分

      或者OR

    • 安装并配置 Azure Powershell,并使用以下命令:Install and configure Azure Powershell, and then use the following command:

      Get-AzureDeployment -ServiceName yourservicename | Select Url
      

      保存任一方法返回的 URL 中所使用的域名,因为创建 CNAME 记录时需要它。Save the domain name used in the URL returned by either method, as you will need it when creating a CNAME record.

  2. 登录到 DNS 注册机构的网站,并转到用于管理 DNS 的页面。Log on to your DNS registrar's website and go to the page for managing DNS. 查找标为“域名”、“DNS”或“名称服务器管理”的站点链接或区域。 Look for links or areas of the site labeled as Domain Name, DNS, or Name Server Management.

  3. 现在找到可以在其中选择或输入 CNAME 记录的位置。Now find where you can select or enter CNAME's. 可能需要从下拉列表中选择记录类型,或者需要转到高级设置页面。You may have to select the record type from a drop down, or go to an advanced settings page. 应查找“CNAME”、“别名”或“子域”字样 。You should look for the words CNAME, Alias, or Subdomains.

  4. 若要为 www.customdomain.com 创建别名,还必须为 CNAME 提供域或子域别名,例如 wwwYou must also provide the domain or subdomain alias for the CNAME, such as www if you want to create an alias for www.customdomain.com. 如果希望为根域创建别名,它可能在注册机构的 DNS 工具中以符号“ @ ”的形式列出。If you want to create an alias for the root domain, it may be listed as the '@' symbol in your registrar's DNS tools.

  5. 然后,必须提供规范主机名,在此示例中为应用程序的 chinacloudapp.cn 域。Then, you must provide a canonical host name, which is your application's chinacloudapp.cn domain in this case.

例如,以下 CNAME 记录会将 www.contoso.com 的全部流量都转发至 contoso.chinacloudapp.cn(已部署应用程序的自定义域名):For example, the following CNAME record forwards all traffic from www.contoso.com to contoso.chinacloudapp.cn, the custom domain name of your deployed application:

别名/主机名/子域Alias/Host name/Subdomain 规范域Canonical domain
wwwwww contoso.chinacloudapp.cncontoso.chinacloudapp.cn

Note

www.contoso.com 的访问者永远不会看到真正的主机 (contoso.chinacloudapp.cn),因此,转发过程对最终用户不可见。A visitor of www.contoso.com will never see the true host (contoso.chinacloudapp.cn), so the forwarding process is invisible to the end user.

上述示例仅适用于 www 子域的流量。The example above only applies to traffic at the www subdomain. 因为无法为 CNAME 记录使用通配符,所以必须为每个域/子域创建一个 CNAME。Since you cannot use wildcards with CNAME records, you must create one CNAME for each domain/subdomain. 若要将子域(例如 *.contoso.com)的流量定向到 chinacloudapp.cn 地址,则可以在 DNS 设置中配置“URL 重定向”或“URL 转发”条目,或者创建一条 A 记录 。If you want to direct traffic from subdomains, such as *.contoso.com, to your chinacloudapp.cn address, you can configure a URL Redirect or URL Forward entry in your DNS settings, or create an A record.

为自定义域添加 A 记录Add an A record for your custom domain

若要创建 A 记录,必须首先找到云服务的虚拟 IP 地址。To create an A record, you must first find the virtual IP address of your cloud service. 然后,使用注册机构所提供的工具在 DNS 表中为自定义域名添加新条目。Then add a new entry in the DNS table for your custom domain by using the tools provided by your registrar. 每个注册机构指定 A 记录的方法类似但略有不同,但概念是相同的。Each registrar has a similar but slightly different method of specifying an A record, but the concepts are the same.

  1. 使用以下方法之一来获取云服务的 IP 地址。Use one of the following methods to get the IP address of your cloud service.

    • 登录到 Azure 门户,选择云服务,查看“概述”部分,然后找到“公共 IP 地址”条目 。Login to the Azure portal, select your cloud service, look at the Overview section and then find the Public IP addresses entry.

      显示 VIP 的速览部分

      或者OR

    • 安装并配置 Azure Powershell,然后使用以下命令:Install and configure Azure Powershell, and then use the following command:

      get-azurevm -servicename yourservicename | get-azureendpoint -VM {$_.VM} | select Vip
      

      保存该 IP 地址,因为创建 A 记录时需要它。Save the IP address, as you will need it when creating an A record.

  2. 登录到 DNS 注册机构的网站,并转到用于管理 DNS 的页面。Log on to your DNS registrar's website and go to the page for managing DNS. 查找标为“域名”、“DNS”或“名称服务器管理”的站点链接或区域。 Look for links or areas of the site labeled as Domain Name, DNS, or Name Server Management.

  3. 然后找到用于选择或输入 A 记录的位置。Now find where you can select or enter A record's. 可能需要从下拉列表中选择记录类型,或者需要转到高级设置页面。You may have to select the record type from a drop down, or go to an advanced settings page.

  4. 选择或输入将使用此 A 记录的域或子域。Select or enter the domain or subdomain that will use this A record. 例如,若要为 www.customdomain.com 创建别名,请选择“www” 。For example, select www if you want to create an alias for www.customdomain.com. 如果希望为所有子域创建通配符条目,请输入“*****”。If you want to create a wildcard entry for all subdomains, enter '*****'. 这将涵盖所有子域,例如 mail.customdomain.comlogin.customdomain.comwww.customdomain.comThis will cover all sub-domains such as mail.customdomain.com, login.customdomain.com, and www.customdomain.com.

    如果希望为根域创建 A 记录,它可能在注册机构的 DNS 工具中以符号“ @ ”的形式列出。If you want to create an A record for the root domain, it may be listed as the '@' symbol in your registrar's DNS tools.

  5. 在提供的字段中输入云服务的 IP 地址。Enter the IP address of your cloud service in the provided field. 这会将 A 记录中使用的域条目与云服务部署的 IP 地址相关联。This associates the domain entry used in the A record with the IP address of your cloud service deployment.

例如,以下 A 记录会将 contoso.com 的全部流量都转发至 137.135.70.239(已部署应用程序的 IP 地址) :For example, the following A record forwards all traffic from contoso.com to 137.135.70.239, the IP address of your deployed application:

主机名/子域Host name/Subdomain IP 地址IP address
@ 137.135.70.239137.135.70.239

此示例展示了如何为根域创建 A 记录。This example demonstrates creating an A record for the root domain. 若要创建一个通配符条目来涵盖所有子域,请输入“*****”作为子域。If you wish to create a wildcard entry to cover all subdomains, you would enter '*****' as the subdomain.

Warning

Azure 中的 IP 地址默认为动态 IP 地址。IP addresses in Azure are dynamic by default. 可能需要使用保留 IP 地址,确保 IP 地址不变。You will probably want to use a reserved IP address to ensure that your IP address does not change.

后续步骤Next steps