How can I eliminate slow resolving/loading of localhost/virtualhost (a 2-3 second lag) on Mac OS X Lion?

I had the exact same problem and it was driving me crazy!

Put all your hosts file entries for localhost into one line like so:

127.0.0.1 localhost myproject.dev myotherproject.dev
::1 localhost
fe80::1%lo0 localhost

Worked like a charm for me. Seems like a bug in Lion.


There's another issue 10.7.* to 10.8.4 for sites ending in .local which causes five second lookups. Details and solution courtesy Bram Van Damme’s blog post found here.

“By default, any hostname ending in .local is treated as a Bonjour host rather than by querying the DNS server entries in Network preferences.”

“To fix this problem (without having to rename each vhost) you need to add IPv6 entries for each of your vhosts in your /etc/hosts file:”

::1 mysite.local
fe80::1%lo0 mysite.local
127.0.0.1 mysite.local

I had the same problem, also on Lion.

Strangely, my solution was the opposite of Jeremy's. I had a whole bunch of someproject.dev entries on one line in /etc/hosts. Loading a site on any of them the first time took forever, like a minute or so. If I used it again within 5 seconds or so it was very fast, but much longer and it would again take a minute. I had suspected all sorts of things, mysql connections, Ruby versions, Rails bugs, Apache, Phusion Passenger. Until I finally looked at the Console and realized that DNS lookups were being attempted.

So, I put all of them on seperate lines:

127.0.0.1 localhost

127.0.0.1 myproject.dev

127.0.0.1 myotherproject.dev

And suddenly everything was snappy again. Same on both my machines.