9

For the purposes of doing web development, I have several domain names specified in my /etc/hosts. These represent several different websites that are being run locally.

127.0.0.1    site1.local
127.0.0.1    site2.local
127.0.0.1    site3.local

This works fine, but I'm seeing a 5 second delay in loading pages from any of these domains. Browser developer tools attribute this delay to DNS, and it's always exactly 5 seconds.

Curiously, this also only happens in Chrome and in Firefox. Safari has no delay at all.

I thought that perhaps the browser is querying the normal DNS servers before falling back to the hosts file. Doing a DNS query with dig site1.local returns no results (as you would expect), but also returns results very quickly (17ms), so that doesn't seem to be the case.

What is causing the 5 second delay in looking up hosts in the /etc/hosts file, and how do I reduce or eliminate this delay?

5

2 Answers 2

16

The 5 second delay when visiting hostnames ending with .local is due to Bonjour, and explained in this answer about a different issue on Stack Overflow.

I was able to use the workaround suggested in that answer and add an IPv6 entry for each server and avoid the 5 second delay. It's unclear why adding both an IPv4 and IPv6 entry causes Bonjour to be avoided, but it does work.

127.0.0.1    site1.local
127.0.0.1    site2.local
127.0.0.1    site3.local
::1    site1.local
::1    site2.local
::1    site3.local
2
  • 2
    Excellent solution! Oct 21, 2020 at 21:00
  • For me, it was a 9.99 second (10 second) delay in Chrome. Fingers crossed that this works!
    – rinogo
    Oct 25, 2021 at 23:20
1

If you are using Apache, Apache is is most likely hanging on DNS lookups for your local development site hostnames.

“I thought that perhaps the browser is querying the normal DNS servers before falling back to the hosts file. Doing a DNS query with dig site1.local returns no results (as you would expect), but also returns results very quickly (17ms), so that doesn't seem to be the case.”

This all sounds like a networking issue to me and the delay seems to be a DNS lookup related issue. Apache is trying to resolve the hostnames for site1.local, site2.local and site3.local and is hanging due to the DNS lookup for those hostnames failing. Then when Apache finally gives up — and gets to the /etc/hosts/ file, the site finally loads.

I go into more detail in my other answer to another question over here, but basically HostnameLookups is a slow process for Apache and when it is part of an Apache config directive, HostnameLookups switches on even if it is disabled elsewhere. So it trues to resolve localhost and just hangs and hangs.

This cleared up many “mystery” hangs on Apache servers that used Allow/Deny directives.

4
  • Thanks for the suggestion, but I'm not using Apache at all.
    – leecbaker
    Oct 21, 2020 at 20:34
  • @leecbaker Then what are you using as a web server? Oct 21, 2020 at 20:36
  • 1
    Most of of the servers are Flask, with a couple of one-off other things. An nginx proxy routes forwards requests to the various servers, all running on different ports.
    – leecbaker
    Oct 21, 2020 at 20:46
  • @leecbaker Ahhh… Makes sense. Happy you found a solution, but I would also recommend looking up how to disable hostname resolution in Nginx and even Flask. The reality is hostname lookups for webservers are typically used for logging purposes. So the only functionality you ever lose by disabling it is having hostnames resolved in logs. Oct 21, 2020 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .