Fixing Slow DNS Queries in OS X Lion

This post is a bit embarrasing. For a long time (from Lion got released, and until a moment ago) I thought that my hosts-file problem in Lion was unsolvable and that it was the fault of Apple.

The issue I had was that when developing on my local machine (PHP 5.3 in Apache), all requests to the webserver were painfully slow. The reason was that all dns queries for my development domains (all ending with .local) went to a remote dns server, before failing, and falling back to the entry from the /etc/hosts file.

Apparently it is because OS X Lion is running IPv6, and thus since my hosts file only contained IPv4 addresses, Lion decided to look up if a IPv6 entry was at the dns server, before falling back to checking the IPv4 in the hosts file.

First fix your hosts file

In your hosts file, you need to provide both IPv4 and IPv6 address for each domain you need to develop on.

127.0.0.1 flow3.local #This was the old line
fe80::1%lo0 flow3.local #This needs to be added as well. 

Second fix your apache configuration

If you previously had your apache virtual hosts ip based, you need to have them only port based.

#Before
NameVirtualHost 127.0.0.1:80

#After
NameVirtualHost *:80

#Before
<VirtualHost 127.0.0.1:80>

#After
<VirtualHost *:80>

Page load times went from 2-3 seconds, to almost instantly.