What’s the Difference Between localhost and 127.0.0.1?

What’s the Difference Between localhost and 127.0.0.1?

What is localhost?

localhost is a domain name, fundamentally no different from the domain names used for internet access, except for its ease of remembrance.

The scope of localhost is limited to the local machine — its name makes it clear: “local” refers to something within the locality.

Both John Smith and Jane Doe can use localhost on their respective machines without interfering with each other, accessing their individual page content.

From Domain Name to Program

To truly understand localhost, we need to discuss how users access programs via domain names,

using google as an example.

1. Upon entering google.com in the browser, it first queries the DNS for the IP address of google.com.

Why the IP address? For analogy, if someone sends a package to your company, the delivery form would include the company’s address, name, and recipient among other details. The delivery process relies on the address for routing, ultimately reaching the recipient. Similarly, in networking, the domain name is like the company name, and the IP address is like the physical address. In the world of networking, the IP address is essential for locating the corresponding program.

DNS acts like a company directory, listing each domain’s corresponding IP address. Some domains might not be registered, making their IP addresses unfindable, while others may have multiple IP addresses, with DNS automatically returning one based on certain rules. After purchasing a domain, domain service providers typically offer a DNS resolution service, registering the domain and its corresponding IP address in the DNS.

Where does the IP address come from? Every online computer has an IP address, but personal computer IPs are generally unsuitable for public access, similar to an internal company location that’s clear to insiders but not to outsiders. For external services like those offered by google, a public IP address is needed, usually provided by an internet service provider. For example, if your company uses China Unicom for internet access, you could have them assign a public IP address to your company’s gateway server. The gateway server acts like a switchboard, handling all internal network communications, with forwarding rules set up to route incoming requests to the appropriate server.

2. With the IP address, the browser sends a request to this address, packaged by the operating system into an IP packet, then transmitted over the network. The network’s routing protocols, based on the provided IP address and through various routers, eventually reach the computer bound to that IP.

3. Multiple network applications might be deployed on a computer. Which one should receive the request? This is where ports come into play. Each network application can bind to one or more ports, with the system preventing overlaps. Specifying a port in the request routes it to the correct network application.

But when we access Google, we don’t specify a port. That’s because the default ports, 80 for HTTP and 443 for HTTPS, are used when none are specified. Binding a port is a must when launching a network program, though some frameworks automatically select an unused port on the computer.

What’s the Difference Between localhost and 127.0.0.1?

With the foundational knowledge from above, we can easily understand this question.

localhost is a domain name, as previously mentioned.

What about 127.0.0.1? It’s an IP address, the local IP address of the current machine, which can only be used on the machine itself. Your computer can use this IP address without being connected to the internet, which is convenient for developing and testing network programs. The programs we debug are bound to this IP address.

It’s worth noting that the IP addresses we commonly see are formatted as X.X.X.X, divided into four segments by dots. In reality, it’s a 32-bit binary number, divided into four 8-bit segments, which are then converted to decimal numbers for display.

How does localhost get resolved to 127.0.0.1 then? Does it go through DNS? No. Every computer can use localhost and 127.0.0.1 without DNS resolution.

This resolution is handled by each computer individually. There’s a hosts file on every computer with some hardcoded DNS resolution rules, including the rule for resolving localhost to 127.0.0.1, which is a convention.

If you prefer not to use localhost, that’s also fine. You can name it anything, like wodehost, and resolve it to 127.0.0.1.

You could even use google.com, but it would only be for your own use, without affecting anyone else.

Domain Name Hierarchy

localhost doesn’t quite resemble the domain names we usually use, such as www.juejin.cn, google.com, or csdn.net. What do www, cn, com, and net mean? And why doesn’t localhost need them?

Domain names are hierarchical, classified into top-level domains (TLDs), second-level domains (SLDs), and third-level domains (3LDs)…

Top-Level Domain (TLD): The highest level in the domain name system, located at the far right of a domain name, usually consisting of a few letters. TLDs are divided into generic TLDs and country-code TLDs, with common generic TLDs including .com for commercial enterprises, .net for network providers, and .org for non-profit organizations, while country-code TLDs represent specific countries or regions, like .cn for China and .uk for the United Kingdom.

Second-Level Domain (SLD): A level below the TLD, chosen and registered by the registrant, can be a personalized and memorable name. For example, juejin.cn is a second-level domain, which is typically what we can apply for. The significance of the top-level domains like .com, .net, or .cn is generally overlooked in favor of brevity and memorability.

Third-Level Domain (3LD): Below the second-level domain, often used to point to specific servers or subnets. In blog.example.com, “blog” is a third-level domain. “www” is the most common third-level domain, representing a website’s homepage or main site, though this is merely a convention, with many sites now recommending direct access via second-level domains.

By this definition, we can consider localhost a top-level domain, albeit a reserved one, solely for accessing the current computer.

Multiple Websites Sharing One IP and Port

As mentioned, different network programs cannot use the same port, but there are ways to circumvent this.

Back when personal blogs were popular, many preferred to purchase a virtual host and deploy an open-source blogging platform to express themselves. To make money, virtual host providers would allocate many virtual hosts on a single computer, allowing everyone to access their domains using the default port 80 without any issues. How is this possible?

If you have experience with web servers like Nginx, Apache, or IIS, you might be familiar with the concept of a host header. A host header is essentially a domain name. By setting host headers, our programs can share one network port.

When deploying a website in web servers like Nginx, we configure it to include the domain name in the host header.

Upon starting, Nginx and other web servers claim port 80 for themselves.

When a website request reaches Nginx’s port 80, it identifies the appropriate network program configured with the corresponding host header based on the domain name in the request.

Nginx then forwards the request to that network program, starting it if necessary.

Private IP Addresses

Apart from 127.0.0.1, there are many private IP addresses, such as the commonly seen 192.168.x.x. These private IP addresses are mostly reserved for use within local area networks (LANs) because allocating a unique IP address to every computer is not feasible. As long as there is no conflict within the LAN, these addresses can be freely used. Your company can use 192.168.1.1, and my company can also use 192.168.1.1, but if you want to access mine, you’d need to go through a public IP address.

The commonly used IPv4 private IP address ranges are divided into three classes:

  • 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 intended for use within internal networks only and cannot be used on the public internet.

In addition to the three private IPv4 address ranges mentioned above, there are also some reserved IPv4 address ranges:

  • The 127.0.0.0 to 127.255.255.255 address range is used for loopback testing, which includes the 127.0.0.1 address mentioned in the question. You can also allocate yourself an IP address like 127.0.0.2, which would function the same as 127.0.0.1.
  • The 169.254.0.0 to 169.254.255.255 address range is used within local networks. This is less commonly encountered; if your computer can’t connect to a LAN, you might see this IP address, which is temporarily assigned as a LAN address.

These address ranges also cannot be used on the public internet.

There are also some less common dedicated IPv4 address ranges. The complete definition of IP address ranges can be found here: www.iana.org/assignments…

IPv6

You might have also heard about IPv6, introduced because the IPv4 address space is insufficient, with too few addresses available. IPv6 could theoretically assign an IP address to every grain of sand on Earth. Despite being talked about for many years, IPv4 remains more widely used. The reasons for this are numerous and won’t be discussed here.

An IPv6 address looks like: XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX. It is 128 bits long, divided into 8 segments by colons, where each X represents a hexadecimal number (ranging from 0 to F). The IPv6 address space is significantly larger than that of IPv4. For example, 2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b is a valid IPv6 address.

Further discussion on IPv6 is beyond the scope of this text, but those interested can explore it further.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了