Home >PHP Framework >Laravel >Hosts file configuration error causes application lag (Laravel-Sail development environment)

Hosts file configuration error causes application lag (Laravel-Sail development environment)

藏色散人
藏色散人forward
2021-06-18 15:47:202675browse

The following tutorial column of laravel will introduce to you the problem of application lag caused by incorrect hosts file configuration (Laravel-Sail development environment). I hope it will be helpful to friends who need it. help!

Today I pulled down the source code of a project and used the new Laravel-Sail development environment. The previous Homestead development environment is now only a backup. Everything went smoothly at first, including dependency installation, container startup, and data migration. Then I happily configured the domain name for development into the hosts file:

127.0.0.1 devocus.test

Open the browser and visit: http ://devocus.test:8016, I thought everything was going well, but I found that the page was always loading, and it took almost 30 seconds for the complete interface to be loaded. I was puzzled. Several other projects I always use the Laravel-Sail development environment, and it almost always starts in seconds. Is this project swollen?

Then I opened the browser developer tools and found that all requests took more than 30 seconds. Did I encounter a file IO problem? But my project is placed in the Ubuntu subsystem. Internet problem? The other items are completely normal. I tried every possible solution, changing file permissions, closing ladder, deleting and re-creating the container, etc., but to no avail.

So I installed Telescope and checked the details of each request. Opening Telescope was also very slow. I waited for about a minute before the Telescope interface finally loaded. But the query results surprised me very much:

记一次 hosts 文件配置错误导致的应用卡顿问题

Telescope shows that each request is processed very quickly, and the data query is also very fast. I am amazed...

So we tried it on a colleague’s computer. The development environment was Laravel-Sail. Unexpectedly, the access on his computer was also very slow. This directly led our thinking to the direction of "there is a problem with the project code", so I spent I spent a whole morning carefully comparing and checking various configuration files and env files, and upgraded laravel and various dependent versions. But every time I open the application in the browser with great expectations, it still freezes and makes me doubt my life, and the log records are all normal. Then use a search engine to try to search for a solution to this problem. There are no results in Chinese or English searches. A morning passed, and I still had not solved this problem, and even a feeling of despair came to my heart.

Finally, I decided to go have lunch and relax myself first. On the way back from dinner, I thought about this question again. Various answers I searched in my head flashed one by one, and suddenly a word "flew" in front of my eyes: hosts.
When I returned to my workstation, I turned on the computer and thought about using IP to access the application directly to see the situation, so I entered http://127.0.0.1:8016 in the browser, and the interface was opened quickly. I was probably a little confused, so I opened the hosts file and thought about changing the domain name:

# 127.0.0.1 devocus.test
127.0.0.1 ifocus-dev.test

Then opened the browser, entered http://ifocus-dev.test:8016, and applied It was opened quickly and smoothly, and the problem was solved inexplicably.
But what’s the problem? I opened the hosts file again, checked carefully, and found that the resolution of the same domain name I had done before was lying quietly in the hosts file. When I migrated the development environment from Homestead to Laravel-Sail, I forgot to modify the hosts file:

...
192.168.10.10 devocus.test
...
# 127.0.0.1 devocus.test
127.0.0.1 ifocus-dev.test

In the hosts file, what will happen if the same domain name is resolved to two IPs? I searched on the search engine and found the answer:

The computer searches for the IP in the hosts file from top to bottom. For the same domain name, take the first IP. If the first IP is blocked, it will switch to the second IP or even the Nth IP. However, this method cannot achieve the load balancing effect, but it can increase the probability of opening the website.

Since I parsed devocus.test to two IP addresses: 192.168.10.10 and 127.0.0.1, when I open http:// in the browser devocus.test, first access 192.168.10.10, but because my Homestead is closed, the access must fail. This process lasts for about 30 seconds, and then the system continues to search for other IPs in the hosts file, finds and The web page was opened only when accessing 127.0.0.1. Because every request has to go through this process, I feel that the system is very stuck. At this point, the problem that troubled me has been solved.

So everyone must pay more attention to these subtle problems when developing projects. Small problems can often cause huge troubles. Even a self-righteous veteran like me will fall into a deep pit. The software industry is really a practical industry that requires constant stepping on pits, digging pits, and filling pits.

But after thinking about it, if I had the Homestead environment open at the time, I might be even more troubled.                                                                                                                                                           #

The above is the detailed content of Hosts file configuration error causes application lag (Laravel-Sail development environment). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete