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

Adam Gries picture Adam Gries · Apr 8, 2012 · Viewed 51.2k times · Source

Since setting up my development environments on Mac OS X Lion (brand new macbook air purchased in January 2012), I have noticed that resolving to a virtual host is very slow (around 3 seconds) the first time but after that is fast as long as I continue loading it regularly.

If I leave it untouched for a couple of minutes and then reload again, the first reload is (again) painfully slow; seems like something is being cached.

As can be seen below I am not using the .local TLD.

My setup: Apache 2 - MySQL - PHP installed and enabled - added a couple of virtual hosts one of which I created for localhost

My /etc/hosts:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
fe80::1%lo0     localhost
127.0.0.1       myproject.dev
::1             myproject.dev
fe80::1%lo0     myproject.dev

My virtual host set-up in username.conf:

NameVirtualHost *:80

<Directory "/Users/myusername/Sites/">
    Options Indexes MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    ServerName localhost
    DocumentRoot /Users/myusername/Dropbox/dev_envs/
</VirtualHost>
<VirtualHost *:80>
    ServerName myproject.dev
    DocumentRoot /Users/myusername/Dropbox/dev_envs/myprojectname
</VirtualHost>

Answer

Jeremy Dunn picture Jeremy Dunn · Apr 18, 2012

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.