Resolving to virtual host very slow on Mac OS X Lion

Since upgrading to Mac OS X Lion (from Snow Leopard), I have noticed that resolving to a virtual host is very slow (between about 3 seconds). I have found a number of tips (e.g., not using the .local TLD) that might resolve this, but they do not apply to my setup.

My setup is quite simple: - Apache 2 (shipped with Lion) - enabled PHP - added a few virtual hosts - installed Mail and SMTP Pear packages

Apache's hosts file looks like this:

127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0 localhost
127.0.0.1   tbi.dev
127.0.0.1   www.tbi.dev
127.0.0.1   test1.tbi.dev
127.0.0.1   test2.tbi.dev
127.0.0.1   psa.dev
127.0.0.1   snd.dev

And Apache's virtual hosts file looks like this:

<VirtualHost *:80>
    DocumentRoot "/Users/Bart/Sites/tbi"
    ServerName tbi.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/Bart/Sites/tbi"
    ServerName tbi.dev
    ServerAlias *.tbi.dev www.tbi.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/Bart/Sites/psa"
    ServerName psa.dev
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/Users/Bart/Sites/sandbox"
    ServerName snd.dev
</VirtualHost>

The setup is basically identical to my setup on Snow Leopard, but Apache's performance for resolving virtual hosts is significantly different. I run Mac OS X Lion 10.7.2, but the issue was already present when running 10.7.1.

This might seem like a small issue, but when you're accessing a virtual hosts a few hundreds of times a day then this adds up to a significant waste of time as you can imagine.


Solution 1:

Long DNS timeouts are almost always a sign of IPv6 issues.

Do you need IPv6 connectivity to apache ?

If not, I suggest changing

<VirtualHost *:80>

into

<VirtualHost 0.0.0.0:80>

Or disable IPv6 connectivity altogether.

Solution 2:

I've run into this just now too.

This will set the IPv6 in Network configuration to Off...

# list all network interfaces to get their names
networksetup -listallnetworkservices
# disable the one you want, in my case it's WiFi
networksetup -setv6off Wi-Fi

But.. unfortunately this didn't solve the DNS resolving issue for me (maybe after system restart). What really helped was to add ipv6 style IPs to /etc/hosts like this:

# my original /etc/hosts ...
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1             localhost 
fe80::1%lo0 localhost

127.0.0.1 project.local

# adding this solved resolving:
fe80::1%lo0 project.local

wget http://project.local now shows instantly

Resolving project.local... 127.0.0.1
Connecting to project.local|127.0.0.1|:80... connected.

instead of hanging for 5 seconds on Resolving project.local.

Solution 3:

On MacOSX Lion .local domain has been "reserved" for Multicast DNS Resolver (bonjour).

This means that looking up any domain ending with .local will result in mDNS lookup (up to 5s) before /etc/hosts.

Fixes:

  1. Change your test domains to some other TLD (i.e. .dev)
  2. Use dscl tool to add an exception.