What is the difference between localhost and 127.0.0.1?

When front-end students debug locally, they probably have to deal with localhost. You only need to execute npm run to open your page window in the browser. The address bar displays this http:// localhost:xxx/index.html Maybe everyone just uses it and hasn't thought about this issue. I think that some of the development students I have worked with before have no idea about the difference between the two, so I think it is necessary to popularize it.

What is the difference between localhost and 127.0.0.1?

When front-end students debug locally, they probably have to deal with localhost. You only need to execute npm run to open your page window in the browser. The address bar displays this http:// localhost:xxx/index.html

Maybe everyone just uses it and hasn't thought about this issue.

I think that some of the development students I have worked with before have no idea about the difference between the two, so I think it is necessary to popularize it.

What is localhost?

localhost is a domain name, which is not fundamentally different from the domain names that everyone uses online. It is just easier to remember.

It’s just that the valid range of localhost is only the local machine. You can also know from the name: local means local.

Both Zhang San and Li Si can use localhost on their respective machines, but they will also get their own page content and will not fight with each other.

From domain name to program

To truly understand localhost, we have to start with how users access the program through domain names.

Take visiting Baidu as an example.

1. When we enter baidu.com in the browser, the browser first queries the IP address of baidu.com in DNS.

Why do you need an IP address? For example, if someone wants to send express delivery to your company, the express delivery form will fill in: the company's mailing address, company name, recipient and other information. During actual transportation, the express delivery will be forwarded layer by layer based on the correspondence address, and finally delivered. into the hands of the recipient. Network communication is similar. The domain name is like a company name, and the IP address is like a communication address. In the network world, the corresponding program can only be found through the IP address.

DNS is like a company yellow page, which records the IP address corresponding to each domain name. Of course, some domain names may not be registered and the corresponding IP address cannot be found. Some domain names may correspond to multiple IP addresses. DNS will Automatically return one according to the rules. After we purchase a domain name, generally the domain name service provider will provide a domain name resolution function, which is to register the domain name and the corresponding IP address into the DNS.

Where did the IP address here come from? Every computer that accesses the Internet will have an IP address, but the IP address of a personal computer is generally not suitable. The IP address of a personal computer is only suitable for intranet positioning, just like which building and floor within your company, and the number of people inside the company. I understand, but if you send it directly to others, they won’t be able to find you. If you want to provide services to the outside world, such as Baidu, you must have a public IP address. This IP address is generally provided by a network service operator. For example, if your company uses China Unicom to access the Internet, you can ask China Unicom to assign one to you. The public IP address is bound to your company's gateway server. The gateway server is like a telephone switchboard. All network communications within the company must pass through it. Then set forwarding rules on the gateway to forward network requests to the provider of network services. on the machine.

2. After having the IP address, the browser will initiate a request to this IP address, package it into an IP request packet through the operating system, and then send it to the network. Network transmission has a complete set of routing protocols. It will be forwarded through layers of routers based on the IP address you provide, and finally reaches the computer bound to the IP.

3. Multiple network applications may be deployed on the computer. Which program should this request be sent to? There is a concept of ports here. Each network application can bind one or more ports when it is started. The ports bound by different network applications cannot be repeated. When binding again, it will prompt that the port is occupied. By specifying the port in the request, the message is sent to the correct network handler.

But there is no input port when we visit Baidu? This is because ports 80 and 443 are used by default without input, http uses 80, and https uses 443. We must bind a port when starting a network program. Of course, some frameworks will automatically select an unused port on the computer.

What is the difference between localhost and 127.0.0.1?

With the above knowledge reserve, we can easily understand this problem.

localhost is the domain name, as mentioned above.

What about 127.0.0.1? It is the IP address, the local IP address of the current machine, and can only be used on this machine. You can use this IP address even if your computer is not connected to the Internet, just to facilitate the development and testing of network programs. The program we start during debugging is bound to this IP address.

Let's briefly talk here. The IP addresses we often see are generally in a format similar to XXXX, with a total of 32 digits, divided into four segments with ".", and expressed in decimal notation.

So how does localhost resolve to 127.0.0.1? Did it go through DNS? No. Each computer can use localhost and 127.0.0.1, which cannot be resolved by DNS.

Then let each computer figure it out on its own. There is a host file on each computer, in which some DNS resolution rules are hard-coded, including the resolution rules from localhost to 127.0.0.1. This is a conventional rule.

If you don't want to use localhost, you can just give it a random name, such as wodehost, and resolve it to 127.0.0.1.

Even if you want to use learntubes.com, you can do it, but you can only enjoy it yourself and have no impact on others

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0