Domain name, IP, DNS, Hosts, Nginx

insert image description here
Let’s start with the most familiar and superficial domain name and IP :
For example, you and me, your name is Zhang San, and my name is Li Si, and the names are similar to domain names. When you play in an Internet cafe, many and too many computers each have a seat number. The seat number is similar to ip. I want to go to an Internet cafe to find you to play on the computer. I only know your name, but I don’t know your location. Therefore, I need DNS.

DNS
DNS refers to a long list of ip, which is hard to remember. Find a way to get a simple name, so its main function is to convert the domain name into a specific ip address, that is, to find your location. I just know the name. You have to find the specific seat ip.
The domain name system is a distributed database that maps domain names and IP addresses to each other, making it easier for people to access the Internet
Note: This domain name can be bound to an ip address, and an ip address can be bound to multiple domain names

HOSTS
let’s talk about hosts, this is a local domain name analysis file, whether you are windwos or linux, there are all of them, if you don’t have one, build one yourself.

Windows is generally in: C:/Windows/System32/drivers/etc/hosts

Linux hosts file path is general: /etc/hosts

example:

127.0.0.1 localhost

127.0.0.1 www.test.com

This file is actually a database configured with the mapping relationship between ip and domain name. What is its function? That is to say, if there is this file locally, the configuration in this file will be used first to parse it. If not, then it will be parsed through nds .

Note: The hosts file can only configure the mapping relationship between ip and domain name, but cannot configure the port number. By default, port 80 is accessed

Because Nginx
needs to solve the port problem, it has to use the famous reverse proxy software: Ngin. As for what Nginx is, how to install and configure it, you can check it in my previous article or search for Du Niang.

Simple example:

//nginx.conf

server {

listen 80; //Listened port

server_name test.com; //Listened domain name

proxy_set_header Cookie $http_cookie; //Header information

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header REMOTE-HOST $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

location / {

proxy_pass http://localhost:8080; //Proxy forwarding, all requests to test.com port 80 will be forwarded to the local port 8080 for processing

}

}

-------------------------------------------------- ----------- Focus -------------------------------------- -----------------
insert image description here

Domain name structure:
http://www.sina.com.cn./

http://hostname.second-level domain.top-level domain. root domain/

The topmost layer of the tree structure is called the root domain, represented by ".", and the corresponding server is called the root server. The resolution right of the entire domain name space belongs to the root server. However, the root server cannot bear a huge load. Some top-level domains are set up under the root domain, and then the resolution rights of different top-level domains are delegated to the corresponding top-level domain servers. For example, the resolution rights of the com domain are delegated to the com domain server. Requests will be forwarded to the com domain server. In the same way, in order to reduce the pressure on the top-level domain, several second-level domains are set up, and the second-level domain has a third-level domain or host.

Question:
When we enter a URL address in the browser, the browser needs to know the IP of the server to send a request to the server corresponding to the host name of the URL. For the browser, the role of DNS is to convert the host name into IP address.

DNS domain name resolution process
DNS domain name resolution, simply put, is to translate the domain name into an IP address. For example: translate the domain name www.baidu.com into the corresponding IP 220.181.38.251, here is just an example
insert image description here
domain name resolution process: (Take Chrome browser as an example)

① The Chrome browser will first search the browser's own DNS cache (the cache time is relatively short, only about 1 minute, and can only accommodate 1000 caches), to see if there is https://www.xxx.com in its own cache entry, and has not expired, if there is and has not expired, the parsing ends here.

Note: How do we view Chrome's own cache? You can use chrome://net-internals/#dns to view

② If the corresponding entry is not found in the browser's own cache, Chrome will search the operating system's own DNS cache, and if it is found and has not expired, the search will stop and the parsing will end here.

Note: How to view the DNS cache of the operating system itself, take Windows system as an example, you can use ipconfig /displaydns to view it on the command line

③ If it is not found in the DNS cache of the Windows system, then try to read the hosts file (located in C:\Windows\System32\drivers\etc) to see if there is an IP address corresponding to the domain name, if there is, the resolution is successful .

④ If no corresponding entry is found in the hosts file. The browser will initiate a DNS system call, and will initiate a domain name resolution request to the locally configured preferred DNS server (usually provided by the telecom operator, or the DNS server provided by Google) (through the UDP protocol to DNS port 53 initiates a request, this request is a recursive request, that is, the operator’s DNS server must provide us with the IP address of the domain name), the operator’s DNS server first searches its own cache, finds the corresponding entry, and If there is no expiration, the parsing is successful.
If no corresponding entry is found, the DNS of the operator initiates an iterative DNS resolution request on behalf of our browser . It will first find the IP address of the DNS of the root domain (this DNS server has built-in 13 IP addresses of the DNS of the root domain) ), find the DNS address of the root domain, and will initiate a request to it (what is the IP address of the domain name www.xxx.com?), the root domain finds that this is a domain name of a top-level domain com domain, so it tells I don’t know the IP address of this domain name in the operator’s DNS, but I know the IP address of the com domain, you go to find it, so the operator’s DNS gets the IP address of the com domain, and initiates an IP address to the com domain IP address The request (what is the IP address of the domain name www.xxx.com?), the server in the com domain tells the operator’s DNS that I don’t know the IP address of the domain name www.xxx.com, but I know xxx.com The DNS address of the domain, you go to find it, so the DNS of the operator initiates a request to the DNS address of the domain name xxx.com (this is generally provided by the domain name registrar, such as Wanwang, Xinnet, etc.) (please ask www What is the IP address of the domain name .xxx.com?), at this time, the DNS server of the xxx.com domain is checked, hey, it is really here, so the found result is sent to the DNS server of the operator, and at this time the operation The DNS server of the merchant gets the IP address corresponding to the domain name www.xxx.com, and returns it to the Windows system kernel, and the kernel returns the result to the browser. Finally, the browser gets the IP address corresponding to www.xxx.com. It's time for a move.

Note: Under normal circumstances, the following steps will not be performed

If after the above 4 steps, the parsing is still not successful, then the following steps will be performed (the following is for the Windows operating system):

⑤ The operating system will search for the NetBIOS name Cache (the NetBIOS name cache exists in the client computer), so what is in this cache? All the computer names and IP addresses of computers that have successfully communicated with me in the recent period of time will be stored in this cache. Under what circumstances can this step be parsed successfully? That is, the name was successfully communicated with me a few minutes ago, so this step can be successfully resolved.

⑥ If the step 5 is not successful, it will query the WINS server (the server corresponding to the NETBIOS name and IP address)

⑦ If the query is not successful in step 6, then the client will perform a broadcast search

⑧ If step 7 is unsuccessful, then the client will read the LMHOSTS file (in the same directory as the HOSTS file, and the writing method is the same)

If the eighth step has not been successfully parsed, then the parsing failure will be declared, and communication with the target computer will not be possible. As long as one of the eight steps can be parsed successfully, it can successfully communicate with the target computer.

Also note:
From the code level, the process of realizing domain name to ip resolution is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/u014365523/article/details/130085730