
What is the difference between localhost and 127.0.0.1?
Introduction to the concepts of localhost and 127.0.0.1 in networking
Currently viewing
Surely those who work in the profession are very familiar with the scene of debugging code locally. Type npm run and the browser will automatically open a website, and the address bar will show something like http://localhost:xxx/index.html. Many people just use it like that without really paying attention to the difference between localhost and 127.0.0.1. Those who work in infrastructure can still grasp it, but many Devs do not clearly distinguish between these two, so those who do not know can read this.
Surely those who work in the profession are very familiar with the scene of debugging code locally. Type npm run and the browser will automatically open a website, and the address bar will show something like http://localhost:xxx/index.html. Many people just use it like that without really paying attention to the difference between localhost and 127.0.0.1. Those who work in infrastructure can still grasp it, but many Devs do not clearly distinguish between these two, so those who do not know can read this.
From Domain to Program: How Does It Work?
To really understand localhost, I need to talk a little about how users access a program through a domain. Take an example. When people type google.com into their browser, the first thing it does is ask the DNS (Domain Name System) what the IP address of google.com is.
Why do we need an IP address? Imagine this: when someone sends a package to your company, the shipping form will have the company address, company name, and recipient name. The delivery person will rely on the address to find the way. In the online world, the domain name (google.com) is like the company name, while the IP address is the physical address. To find the right program, you need an IP address. DNS is like a giant phone book, listing the corresponding IP address for each domain name. After people buy a domain name, the provider will help register the domain name and the server's IP address into this DNS.
With the IP address, the browser will send a request to that address. This request is packaged by the operating system and transmitted over the network. Routers on the internet will rely on this IP address to find the path and finally send the packet to the correct computer holding that IP.
A computer can run many network programs. So how can the request be sent to the correct application? This is where ports come into play. Each network program can "listen" on one or more ports, and the system will not allow two programs to occupy the same port. When sending a request, I specify an additional port so that it goes to the correct program.
But why don't I need to type the port when I go to Google? That's because there are default ports: 80 for HTTP and 443 for HTTPS. When I don't specify a port, the browser will use these ports automatically.
Vậy Localhost và 127.0.0.1 khác nhau chỗ nào?
- 1. What is Localhost?
Localhost is a domain name. Basically, it is no different from google.com or other domains that people often access, except that it is more special and easier to remember. The scope of localhost is limited to the computer that everyone is using. Both Mr. A and Mr. B can use localhost on their computers without touching each other, each person will access the website on their own computer.
- 2. What is 127.0.0.1?
127.0.0.1 is an IP address. Specifically, it is a special IP address, called a loopback address, used to refer to the current computer itself. People can use this IP address even when the computer is not connected to the internet, which is very convenient for developing and testing programs. Your debug programs are running and "listening" on this IP address.
So how is localhost resolved to **127.0.0.1**? Do you need to ask DNS? - No.
This resolution is handled right on each person's computer. On every computer, there is a file called hosts, this file contains a few hardcoded DNS resolution rules. In which, the default rule is to resolve localhost to 127.0.0.1. This is a common convention.
If you like, you can edit the hosts file to give it a different name, for example maycuatoi, and point it to 127.0.0.1. You can even point google.com to 127.0.0.1, but it only works on your computer, not on anyone else.
Running multiple websites on the same IP and Port - Different network programs cannot share the same port, but there are ways to get around the rules.
In the old days, virtual host providers could cram dozens or hundreds of websites on the same physical server, and everyone accessed their site through the default port 80. How did they do that?
If you have ever worked with a web server like Nginx, Apache, or IIS, you will be familiar with the concept of a host header. The host header is essentially a domain. By setting the host header, programs can share the same network port.
- 3. How it works
When starting, Nginx will take over port 80 for itself. When a website request arrives at Nginx's port 80, it will look at the domain in the request (which is the host header). Based on this domain, Nginx will know which website this request is for that has been configured in it. Nginx will then forward the request to the corresponding web. Private IP Address In addition to 127.0.0.1, there are many other private IP addresses, such as the very familiar 192.168.x.x range. These IP addresses are reserved for use in the internal network (LAN). Your company can use the address 192.168.1.1, and my company can also use 192.168.1.1 without conflict.
Common private IP ranges of IPv4 include:
Class A:10.0.0.0 to 10.255.255.255Class B:172.16.0.0 to 172.31.255.255Class C:192.168.0.0 to 192.168.255.255
There are also some other reserved IP ranges:
- The range 127.0.0.0 to 127.255.255.255 is used for loopback testing, which includes the address 127.0.0.1. People assigning themselves the IP 127.0.0.2 will work exactly the same as 127.0.0.1.
- The range 169.254.0.0 to 169.254.255.255 is used when the computer is not connected to the LAN and cannot receive an IP from the DHCP server. About IPv6
Everyone may have heard of IPv6, which was introduced because the IPv4 address space was exhausted. In theory, IPv6 can assign an IP address to every grain of sand on Earth. Although it has been talked about for many years, IPv4 is still more popular for many reasons.
An IPv6 address looks like this: 2001:0db8:3c4d:0015:0000:0000:1a2f:1a2b. It is 128 bits long, much longer than IPv4's 32 bits.
🍃 Related posts
Want to update?
Subscribe to my blog to receive the latest updates from us.