原文:https://ns1.com/resources/cname
域名系统 (DNS) 中使用规范名称 (CNAME) 记录来创建从一个域名到另一个域名的别名。一个常见的例子是 www 子域,它作为根域名的别名提供 – 访问“www.example.com”的用户被称为根域(或 DNS 区域顶点)“example.com”。
别名记录的一些常见用途是:
为特定网络服务(如电子邮件或 FTP)提供单独的主机名,并将该主机名指向根域
许多托管服务为服务提供商域上的每个客户提供子域(例如 company.hostname.com),并使用 CNAME 指向客户的域 (www.company.com)。
在多个国家/地区注册同一域名,并将国家/地区版本指向主“.com”域名
从同一组织拥有的多个网站指向主网站
DNS 系统如何处理别名记录
上面示例中的 DNS 记录如下所示:
从子域到父域的别名记录
名称类型值
————————————————–
www.example.com。别名 example.com。
example.com。至 192,162,100,100
第二条记录是 A 记录,它将人类可读的域名“example.com”转换为 IP 地址。
别名记录的 DNS 解析过程
DNS 客户端(如浏览器或网络设备)请求地址 www.example.com,并创建 DNS 请求。
DNS 解析器接收请求并查找保存 DNS 区域文件以及“example.com”域的 DNS 记录的权威名称服务器。
解析 DNS 请求,并将 CNAME 记录返回到客户端。
客户端理解 www.example.com 只是真实地址“example.com”的别名,并为“example.com”发出新的 DNS 查询
重复该过程,解析器返回包含 IP 地址的“example.com”的 A 记录。
DNS 客户端现在使用其 IP 地址连接到“example.com”。
别名记录示例
从子域到子域的别名记录
名称类型值
————————————————–
old.example.com。别名 new.example.com。
new.example.com。至 192.162.100.101
从子域到其他根域的别名记录
名称类型值
————————————————–
other.example.com。别名 www.other.com。
在此示例中,“www.other.com”的名称在另一个 DNS 区域文件中提供。
对别名记录的限制
CNAME 不能放置在根域级别,因为根域是 DNS 授权机构起始 (SOA),它必须指向 IP 地址。
CNAME 记录必须指向另一个域名,而不是指向 IP 地址。
在 CNAME 记录中定义的主机名不得有其他类型的其他资源记录(MX、A 等),但 RRSIG 和 NSEC 等 DNSSEC 记录除外。
CNAME 记录可以指向其他 CNAME 记录,但这不被视为一种好的做法,因为它效率低下。
MX 和 NS 记录绝不能指向别名。
用于电子邮件的域可能没有 CNAME 记录 – 这可能会对不同的邮件服务器产生不良结果。
别名记录和替代记录类型
CNAME 记录通常与其他类型的 DNS 记录(A 记录和别名记录)一起使用。
A 和 CNAME 之间的区别
A 记录将主机名映射到一个或多个 IP 地址,而 CNAME 记录将主机名映射到另一个主机名。
别名和别名之间的区别
别名记录(如 CNAME)也将主机名映射到另一个主机名。但是,别名记录使得在同一主机名上具有其他 DNS 记录成为可能,而 CNAME 则没有。这样就可以在根域(DNS 区域顶点)应用别名,这是 CNAME 不允许的。
此外,ALIAS 的性能比 CNAME 更好,因为它不需要 DNS 客户端解析另一个主机名 – 它直接返回一个 IP。但是,ALIAS 记录也需要在后台执行递归查找,这可能会影响性能。
A Canonical Name (CNAME) Record is used in the Domain Name System (DNS) to create an alias from one domain name to another domain name. A common example is the www subdomain which is provided as an alias to the root domain name – users accessing “www.example.com” are referred to the root domain (or DNS zone apex) “example.com”.
A few common uses of CNAME records are:
- Providing a separate hostname for specific network services, such as email or FTP, and pointing that hostname to the root domain
- Many hosted services provide a subdomain for each customer on the service provider’s domain (e.g. company.hostname.com), and use CNAME to point to the customer’s domain (www.company.com).
- Registering the same domain in several countries and pointing the country versions to the main “.com” domain
- Pointing from several websites owned by the same organization to a primary website
How the DNS System Handles CNAME Records
The DNS records in the above example would look like this:
CNAME from subdomain to parent domain
NAME TYPE VALUE
————————————————–
www.example.com. CNAME example.com.
example.com. A 192.162.100.100
The second record is an A record which translates the human-readable domain name “example.com” to an IP address.
DNS Resolution Process for CNAME Records
- A DNS client (such as a browser or network device) requests the address www.example.com, and a DNS request is created.
- A DNS resolver receives the request and finds the Authoritative Name Server that holds the DNS Zone file with DNS records for the “example.com” domain.
- The DNS request is resolved and the CNAME record is returned to the client.
- The client understands www.example.com is only an alias for the real address, “example.com”, and issues a new DNS query for “example.com”
- The process is repeated and the resolver returns the A record for “example.com”, containing the IP address.
- The DNS client now connects to “example.com” using its IP address.
CNAME examples
CNAME from subdomain to subdomain
NAME TYPE VALUE
————————————————–
old.example.com. CNAME new.example.com.
new.example.com. A 192.162.100.101
CNAME from subdomain to other root domain
NAME TYPE VALUE
————————————————–
other.example.com. CNAME www.other.com.
In this example the A name for “www.other.com” is provided in another DNS Zone file.
Restrictions on CNAME Records
- A CNAME cannot be placed at the root domain level, because the root domain is the DNS Start of Authority (SOA) which must point to an IP address.
- CNAME records must point to another domain name, never to an IP address.
- A hostname defined in a CNAME record must have no other resource records of other types (MX, A, etc.), except for DNSSEC records like RRSIG and NSEC.
- CNAME records can point to other CNAME records, but this is not considered a good practice as it is inefficient.
- MX and NS records must never point to a CNAME alias.
- Domains that are used for e-mail may not have a CNAME record – this can have undesirable results with different mail servers.
CNAMEs and Alternative Record Types
The CNAME Record is typically used alongside other types of DNS Records – A Records and ALIAS Records.
Difference Between A and CNAME
An A Record maps a hostname to one or more IP addresses, while the CNAME record maps a hostname to another hostname.
Difference between ALIAS and CNAME
The ALIAS Record, like CNAME, also maps a hostname to another hostname. However, the ALIAS Record makes it possible to have other DNS records on the same hostname, while CNAME does not. This makes it possible to apply ALIAS at the root domain (DNS zone apex), which is not allowed for CNAME.
In addition, ALIAS has better performance than CNAME because it does not require the DNS client to resolve another hostname – it directly returns an IP. However ALIAS records too need to do recursive lookups behind the scenes which can affect performance.