Long delay on contacting service on localhost using /etc/hosts entry

5

2

I have a web server running on my local machine and when I perform a cURL request against it using the localhost address it executes very fast. However, when I add an /etc/hosts like

127.0.0.1 test.local

and then perform the same cURL request:

curl -I http://test.local:3002/images/logo.png

it delays for a solid 2-3 seconds. Changing the cURL request back to localhost again yields no delay.

This is reproducible every single time.

I've tried running the slow request under dtruss to see if I can find a system call that is causing the delay. Nothing out of the ordinary shows up.

I've tried making a dummy service via netcat that just returns HTTP/1.0 200 OK so I can guarantee the issue is not really due to my web server. Again, no difference: localhost is fast as you'd expect and test.local delays.

At this point I think there is somehow a DNS issue. That is, /etc/hosts is NOT being consulted first, that is, my upstream DNS is being checked, which fails, then /etc/hosts is being consulted which finally works.

Is this possible? Is OS X Lion somehow consulting my /etc/hosts later in the resolver chain?

Cody Caughlan

Posted 2011-08-05T21:46:15.300

Reputation: 211

Answers

6

Ok, I figured it out. I found this comment on a blog post:

The matter is Lion handles .local TLD differently because it’s reserved for some Multicast DNS features (used by Bonjour). The only way i found to solve this issue is using a different TLD for development hosts (ie: .dev). It works fine for me, hope it’s gonna be helpful to others!

Switching my development domain to end in .dev works fine.

tl;dr - dont use .local for development domains in Lion.

Cody Caughlan

Posted 2011-08-05T21:46:15.300

Reputation: 211

You should add that if you add the IP6 line to hosts (::1 test.local), it will fix the issue. – Matt Alexander – 2014-04-23T19:51:36.487

1

OSX lion has this known bug. It would consult the DNS server first before using /etc/hosts entries. This behavior is only found in OSX lion and is not found in snow leopard, or even in tiger.

Possible options:

  • Install DNSMasq
  • Wait for patch from macosx
  • Use snow leopard for the time being

Also see http://www.justincarmony.com/blog/2011/07/27/mac-os-x-lion-etc-hosts-bugs-and-dns-resolution/ for more details. It's well covered there.

bubu

Posted 2011-08-05T21:46:15.300

Reputation: 9 283