14

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?

leecbaker
  • 593

2 Answers2

26

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
Giacomo1968
  • 58,727
leecbaker
  • 593
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.

Giacomo1968
  • 58,727