Thoroughly understood! Localhost is different from 127.0.0.1, programmers must know!
In the process of network development and O&M, localhost and 127.0.0.1 are two concepts that often appear and are often considered to be equivalent. However, while localhost and 127.0.0.1 implement a similar function in most cases, i.e., access to native resources, there are key differences between the two. localhost is a domain name that points to a local computer, while 127.0.0.1 is a standardized IP address. In terms of DNS resolution, system configuration, and performance, the two have different working methods and applicable scenarios. A deeper understanding of the differences between localhost and 127.0.0.1 will help us make more reasonable choices in network development and configuration, and improve the stability and security of the system.
When a front-end developer debugs locally
Typically, they'll interact with localhost and simply run npm run to open a web page in their browser with something like http://localhost:xxx/index.html in the address bar.
Many people may not think about the difference when they use it.
Considering that when I worked with developers in the past, they weren't very clear about the differences, so I felt the need to spread some knowledge.
What is localhost?
localhost is a domain name that is essentially the same as the one you use to access the internet, except that it's easy to remember.
The scope of localhost is limited to the local computer – as the name suggests, "local" means something local.
Xiao Ming and Li Fei can use localhost on their respective computers without interfering with each other and access their respective page content.
From the domain name to the program
In order to truly understand localhost, we need to discuss how users can access the program through domain names, using Google as an example.
1. When you type google.com into your browser, DNS is first queried for the IP address of the google.com.
Why IP addresses? For example, if someone sends a package to your company, the delivery slip will contain information such as the company's address, name, and recipient. The express delivery process mainly relies on the address for delivery, and finally reaches the recipient. Similarly, in the web, a domain name is like a company name, while an IP address is like a physical address. In the online world, IP addresses are the key to locating the appropriate program.
DNS is like a company directory that lists the IP addresses for each domain name. Some domain names may not be registered, making it impossible to find their IP address; Others may have multiple IP addresses, and DNS will automatically return one of them based on a certain rule. When a domain name is purchased, a domain name service provider typically provides a DNS resolution service that registers the domain name and its corresponding IP address in the DNS.
Where do IP addresses come from? Every networked computer has an IP address, but the IP of a personal computer is usually not publicly accessible, similar to an address within a company, which is clear to insiders but not to outsiders. For external services like those provided by Google, a public IP address is required, usually provided by an internet service provider. For example, if your company uses China Unicom to access the Internet, you can ask them to assign a public IP address to your company's gateway server. The gateway server acts like a switchboard, handling all internal network communications and setting up forwarding rules to route external requests to the appropriate servers.
2. Once the IP address is obtained, the browser sends a request to the address, which is encapsulated by the operating system as an IP packet and then transmitted over the network. Depending on the IP address provided, the routing protocol in the network passes through each router and eventually arrives at the computer bound to that IP.
3. Multiple web applications may be deployed on a single computer. Which app should receive the request? This is where ports come in handy. Each network application can be bound to one or more ports, and the system prevents duplicate ports. The port specified in the request routes the request to the correct network application.
However, when accessing Google, we don't specify a port because the default port is 80 (HTTP) or 443 (HTTPS), which is used when no port is specified. Ports must be bound when you start a network program, although some frameworks automatically select ports that are not occupied on your computer.
What is the difference between localhost and 127.0.0.1?
With the above basics in mind, we can easily understand the question.
localhost is a domain name, as mentioned earlier.
What about 127.0.0.1? It is an IP address, which is the local IP address of the machine and can only be used on the machine. Your computer can use this IP address without an internet connection, which is convenient for developing and testing network programs. The program we debug is bound to this IP address.
It's worth noting that our common IP address format is X.X.X.X, separated by dots into four parts. Actually, it is a 32-bit binary number that is divided into four 8-bit parts and then converted to a decimal number for display.
So how does localhost resolve to 127.0.0.1? Is it through DNS? Not. Each machine can use localhost and 127.0.0.1 without DNS resolution.
This parsing is handled independently by each computer. On each machine there is a hosts file with some hard-coded DNS resolution rules, including the rule to resolve localhost to 127.0.0.1, which is a convention.
If you don't want to use localhost, that's fine. You can name it whatever you want, like wodehost, and resolve it to 127.0.0.1.
You can even use google.com, but only for your personal use and without affecting others.
Domain name hierarchy
localhost isn't quite like the domain name we usually use, such as www.juejin.cn, google.com, or csdn.net. So, what do www, cn, com, and net mean? Why doesn't localhost need these parts?
Domain names are hierarchical and are usually divided into top-level domains (TLDs), second-level domains (SLDs), and third-level domains (3LDs), among others...
Top-level domain (TLD): This is the highest level in the Domain Name System, located at the far right of a domain name, and usually consists of a few letters. Top-level domains are divided into gTLDs and ccTLDs. Common gTLDs are .com (for commercial enterprises), . .net (for network providers) and .org (for non-profit organizations), while country code top-level domains represent specific countries or regions, such as .cn for China and .uk for the United Kingdom.
Second-Level Domain Name (SLD): Located one level below the top-level domain, it is selected and registered by the registrant and can be a personalized and easy-to-remember name. For example, juejin.cn is a second-level domain name, which is the level at which we can usually apply for a domain name. Top-level domains such as .com, . The meaning of .net or .cn is often simplified to improve the simplicity and memorability of domain names.
Third-level domain name (3LD): Located below the second-level domain name, it is usually used to point to a specific server or subnet. For example, in blog.example.com, "blog" is a third-level domain name. "www" is the most common third-level domain name, which means the home page or main site of a website, but this is just the norm, and many websites now recommend accessing directly through the second-level domain.
From this definition, we can think of localhost as a top-level domain, although it is reserved and dedicated to accessing the current computer.
Multiple websites share a single IP and port
As mentioned earlier, different network programs can't use the same port, but there are ways to get around this limitation.
At a time when personal blogging is popular, many people prefer to buy web hosting and deploy an open-source blogging platform to express themselves. To make a profit, a web hosting provider allocates multiple web hosts on a single computer, allowing everyone to access their domain name using the default port 80 without conflicts. How?
If you're familiar with web servers like Nginx, Apache, or IIS, you probably know a little bit about the "host head" concept. The host header is actually the domain name. By setting the host header, our programs can share the same network port.
When we deploy a website in a web server such as Nginx, the configuration file will contain the domain name information of the host header.
Once booted, Nginx and other web servers occupy port 80.
When the website request reaches port 80 of Nginx, Nginx will find the corresponding network program based on the domain name information in the request, forward the request to the corresponding network program, and start the program (if necessary).
Private IP address
In addition to 127.0.0.1, there are many private IP addresses, such as the common 192.168.x.x. These private IP addresses are primarily used by local area networks (LANs) because it is not practical to assign a unique IP address to each computer. As long as there are no conflicts within the local area network, these addresses can be used freely. Your company can use 192.168.1.1, and my company can use 192.168.1.1, but if you want to access my network, you'll need to use a public IP address.
There are three types of commonly used IPv4 private IP address ranges:
- Class A: from 10.0.0.0 to 10.255.255.255
- Class B: From 172.16.0.0 to 172.31.255.255
- Class C: from 192.168.0.0 to 192.168.255.255
These private IP addresses are only used by the internal network and cannot be used on the public network.
In addition to the above three types of private IPv4 address ranges, there are also some reserved IPv4 address ranges:
- The address range 127.0.0.0 to 127.255.255.255 is used for loopback testing, including the 127.0.0.1 address mentioned in the question. You can also assign yourself an IP address like 127.0.0.2, which functions just like 127.0.0.1.
- The 169.254.0.0 to 169.254.255.255 address range is used for on-premises networks. This is less common; If your computer can't connect to a local area network, you might see the IP address, temporarily acting as a local area network address.
These address ranges are also not available on the Internet.
There are also some less common private IPv4 address ranges. The full definition of IP address ranges can be found at: www.iana.org/assignments...
IPv6
You may have also heard of IPv6, which was introduced due to a lack of IPv4 address space because of the limited number of IPv4 addresses. IPv6 can theoretically assign an IP address to every grain of sand on the planet. Although IPv6 has been discussed for many years, IPv4 is still more widely used. There are many reasons for this, and I won't go into them here.
The format of an IPv6 address is: XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX
。 It is 128 bits long and is separated by a colon into 8 segments, where each X represents a hexadecimal number (ranging from 0 to F). The address space of IPv6 is much larger than that of IPv4, for example, 2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b is a valid IPv6 address.
Conclusion:
The common features of localhost and 127.0.0.1 in local network access make them often regarded as synonyms, but from the perspective of network protocols, the two are fundamentally different. localhost is implemented through the DNS resolution of the operating system, while 127.0.0.1 is used as the loopback address defined in the IPv4 protocol standard, pointing directly to the native IP. This means that in some network and system environments, localhost may fail to resolve due to DNS configuration or network failures, and 127.0.0.1 is often able to bypass these issues and access native services directly. In addition, localhost supports IPv6 resolution, which resolves to: :1 (IPv6 loopback address), which provides support for native access in future IPv6 environments. Understanding and using localhost and 127.0.0.1 not only helps optimize the debugging experience of local development, but also ensures a stable connection to local services in complex network environments.