All developers unconsciously accept and use the concept that localhost == 127.0.0.1, just like the mathematical notion of 1+1=2. However, just as mathematicians attempt to prove why 1+1 equals 2, I suddenly became curious about this aspect today. Thus, I delved into the principle behind it and came to a new understanding of the communication mechanism in Linux. This highlighted the logical rigor of developers, and I could not help but be amazed during the process of uncovering the principle. Below is a summary of what I learned during this process.

1. How does localhost connect to 127.0.0.1?

Question: Why does calling localhost connect to 127.0.0.1?

Answer: When a network request is made, the Linux system converts the domain name into an IP address. In this process, it first checks the /etc/hosts file. By default, the /etc/hosts file includes the following configuration:

127.0.0.1    localhost

Here, 127.0.0.1 refers to the loopback interface, meaning it refers to itself. Therefore, when you call http://localhost from your browser or terminal, Linux connects this request directly to 127.0.0.1 through the /etc/hosts file without going through DNS. As a result, the name localhost functions internally in Linux as an alias representing itself.

2. What is the /etc/hosts file and what role does it play?

Question: How exactly does the /etc/hosts file work?

Answer: The /etc/hosts file is one of the most fundamental mechanisms in the hostname resolution process of Linux. When a network request occurs, Linux converts the domain name to an IP address in the following order:

  1. Check /etc/hosts file: The getaddrinfo() function provided by Linux's glibc library is the starting point for all network requests. This function checks the /etc/hosts file first when interpreting a domain's IP address. If the domain is defined in /etc/hosts, Linux uses this information to return the IP address directly without going through the DNS server.
  2. Request to DNS server: If the domain is not defined in the /etc/hosts file, it looks up the IP address through the configured DNS server.

The /etc/hosts file applies to all network requests and operates the same way across various network request libraries such as browsers, curl, ping, and Python's requests module.

3. What can be achieved by utilizing /etc/hosts?

Question: What effect would adding a domain name to the /etc/hosts file have?

Answer: By adding a desired domain name and IP address to the /etc/hosts file, it connects directly to the specified IP without going through DNS when the domain is requested. For example:

127.0.0.1    localhost
127.0.0.1    app1.local
127.0.0.1    app2.local

With this setup:

  • Requests to app1.local and app2.local will be routed to 127.0.0.1 by Linux.
  • It's useful for virtual domain testing or internal communication between applications in a development environment.
  • The requested domains have the same effect as localhost but can be handled independently.

4. How do Nginx and Gunicorn collaborate with /etc/hosts?

Question: Can servers like Nginx and Gunicorn interact with the /etc/hosts file?

Answer: Since web servers like Nginx operate above the Linux networking layer, they follow the domains set in /etc/hosts. For example:

  • If 127.0.0.1 app1.local is set in /etc/hosts, Nginx routes requests for this domain to the application mapped to 127.0.0.1.
  • You can set up virtual hosts in Nginx to separate requests by domain. Example:
server {
    server_name app1.local;
    location / {
        proxy_pass http://127.0.0.1:8000;  # app1 service
    }
}

server {
    server_name app2.local;
    location / {
        proxy_pass http://127.0.0.1:8001;  # app2 service
    }
}

This configuration allows running two applications independently on the same server.

5. How is /etc/hosts useful in development environments?

Question: In what situations is the /etc/hosts file actually useful?

Answer: The /etc/hosts file is particularly useful in development and testing environments in the following situations:

  • Local domain testing: You can test specific domains in a local environment. For example, mapping https://test.local to a local application.
  • DNS bypass: Temporarily connect to a different IP without actual DNS server settings. For example, routing example.com to another server IP.
  • Internal communication between applications: Using /etc/hosts for communication between multiple applications running on the same server reduces DNS requests and optimizes communication speed.

6. The value of /etc/hosts in dynamic IP and DNS cache issues

Personal experience: Developers using dynamic IPs generally solve IP change issues through DDNS (Dynamic DNS). However, there is one problem: Linux hosts maintain a DNS cache, causing them to continue using the previous IP until the TTL (Time to Live) expires. This can lead to issues where applications send TCP requests to past IPs unless the cache is manually cleared with the sudo resolvectl flush-caches command.

To solve this problem, using /etc/hosts enables routing a specific domain to a fixed IP, bypassing DNS altogether. Through this, I found a way to fundamentally prevent communication issues caused by IP changes. The fact that such a simple configuration can be key to resolving issues is quite impressive.

In Conclusion

Through all this, I realized that the localhost and the /etc/hosts file I unconsciously used are closely connected to the fundamental networking mechanism of Linux. Feeling the logical rigor with which Linux developers designed such fundamental structures fills me with awe. It's surprising that a single simple file can play such an important role in both development and production environments.

This experience reaffirmed how beneficial it is to understand the essence of technology. 😊

Illustration of the /etc/hosts file usage