Before we get started, let’s quickly go over some of the terms we’ll be using later in the article:
- Domain name: the full name used to access the site. For example,
tihandev.com
orwww.tihandev.com
. - Top-level domain: the last part of the domain name. For example, the .com part of
tihandev.com
. - The Apex domain (or root domain): part of
tihandev.com
atwww.tihandev.com
. - Subdomain: a domain that is part of a larger domain. For example,
www.tihandev.com
andapi.tihandev.com
are subdomains oftihandev.com
. - Domain registrar (or domain registration service): a company that allows you to register a domain name. For example, https://www.namecheap.com or https://www.godaddy.com.
- DNS provider: The company that maintains the DNS servers that translate the domain name to the destination.
- Name Server (or DNS Server): A specialized server that translates domain names into IP addresses.
What is an A record?
A record maps the domain to the physical IP address of the computer hosting the domain. Internet traffic uses A record to find the computer that contains the DNS domain settings. The A record value is always an IP address, and multiple A records can be configured for the same domain name.
For example, you can use it for tihandev.com
or www.tihandev.com
and specify where you have the app.
What is a CNAME record?
A CNAME record - also known as a canonical name record - is one of many types of resource records in DNS. In the simplest sense, a CNAME record maps one domain name to another.
For example, api.tihandev.com
is an alias for tihandev.com
. CNAME example:
Difference between A and CNAME records
In all cases, the canonical name (CNAME) must always be another domain name and can never be an IP address. This is the biggest difference between CNAME records and A records.
A records map domain names to IP addresses. A record example: While CNAME records map domain names to other domain names. CNAME subdomain example:
Why is a CNAME record needed?
This can come in handy when you have multiple services
running on a single server with the same IP address
like a website and an API server. In this case, you can use a CNAME record to point api.tihandev.com
and www.tihandev.com
to the DNS entry for tihandev.com
which in turn has its A record that points to the IP address for this server.
Then in the future, if the IP address ever changes, you only need to change the A record.
Creating CNAME and A records
Let’s say you bought the domain name tihandev.com
and now you want to use it for your application.
To create CNAME and A records go to your domain registrar, in this case, we will use Namecheap, but with other registrars, the process is almost the same.
- First, log in to Namecheap and go to the dashboard.
- Click the Domain List section, select your domain and click the Manage button.
- Click the Advanced DNS tab.
- Click the Add New Record button.
- Select A record from the drop-down menu for the Type, Host field fill with @ - which is a shortcut for the name of the root domain (in our case, it means
tihandev.com
) and Value field fill with the IP address itself (for example,75.2.60.5
). - For the CNAME record, click the Add New Record button again.
- Select the CNAME record from the drop-down menu, set the host to the desired host (for example, api or www), and in the Value field enter the name of the root domain (for example,
tihandev.com
). - Click the Save All Changes button.
Usually, after a few minutes or hours, you can request a domain name (eg tihandev.com
) and its subdomains (eg api.tihandev.com
, www.tihandev.com
).
Check DNS records with “dnschecker”
To check that DNS records are fully propagated, you can do this using a website such as dnschecker.org.
Check DNS records with the “dig command”
You can also use the dig command from a terminal to find A and CNAME records.
The following request tells us that tihandev.com
is referencing the IP address 75.2.60.5.
And this request tells us that api.tihandev.com
refers to tihandev.com
.