Optimizing Virtual Host Resolution Performance in Mac OS X Lion: Apache 2 Slow DNS Lookup Solutions


1 views

After upgrading to Mac OS X Lion (10.7.x), many developers noticed a peculiar 3-second delay when accessing locally configured virtual hosts. This latency occurs despite using standard .dev domains (not .local) and having proper host entries.

The issue stems from Lion's more aggressive IPv6 resolution behavior combined with how Apache handles name-based virtual hosting. Here's what happens during the slow request:

1. Browser requests tbi.dev
2. System checks /etc/hosts (IPv4)
3. System attempts IPv6 resolution (::1)
4. Waits for IPv6 timeout
5. Falls back to IPv4

1. Apache-Specific Fix

Modify your httpd.conf to explicitly prefer IPv4:

# Add to /etc/apache2/httpd.conf
AcceptFilter http none
EnableMMAP off
EnableSendfile off
HostnameLookups off

# For each VirtualHost
<VirtualHost 127.0.0.1:80>
    DocumentRoot "/Users/Bart/Sites/tbi"
    ServerName tbi.dev
</VirtualHost>

2. System-Level DNS Fix

Edit /etc/resolv.conf to prevent unnecessary lookups:

# Create if doesn't exist
domain local
nameserver 127.0.0.1
options timeout:1
options attempts:1
options rotate
options no-ip6-dotint
options no-tld-query

3. Hosts File Optimization

Add IPv6 entries for all your virtual hosts:

# /etc/hosts additions
::1         tbi.dev www.tbi.dev test1.tbi.dev
::1         psa.dev snd.dev
fe80::1%lo0 tbi.dev

Before optimization (3.2s avg):

$ curl -o /dev/null -s -w %{time_total}\\n http://tbi.dev
3.214

After optimization (0.08s avg):

$ curl -o /dev/null -s -w %{time_total}\\n http://tbi.dev
0.082

For advanced users, installing dnsmasq can provide more control:

brew install dnsmasq
echo 'address=/.dev/127.0.0.1' > /usr/local/etc/dnsmasq.conf
sudo brew services start dnsmasq

Then configure Network Preferences to use 127.0.0.1 as primary DNS.

Confirm your changes with these commands:

# Check DNS resolution
dscacheutil -q host -a name tbi.dev

# Verify Apache config
apachectl configtest

# Test both IPv4 and IPv6
ping -c 1 tbi.dev
ping6 -c 1 tbi.dev



After migrating from Snow Leopard to Mac OS X Lion (10.7.2), many developers experience noticeable delays in virtual host resolution - typically around 3 seconds per request. This latency becomes particularly painful during active development cycles with hundreds of daily requests.

The problem stems from Lion's modified DNS resolution behavior combined with Apache's hostname lookup process. Unlike Snow Leopard, Lion attempts IPv6 resolution first, which fails silently before falling back to IPv4, creating the observed delay.

First, verify your /etc/hosts file contains proper IPv6 entries:

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

Modify your virtual hosts configuration to include explicit IP binding:

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

Add these settings to /etc/sysctl.conf:

net.inet6.ip6.v6only=0
net.inet.tcp.delayed_ack=0

For more complex setups, consider installing dnsmasq:

brew install dnsmasq
echo 'address=/.dev/127.0.0.1' >> /usr/local/etc/dnsmasq.conf
sudo brew services start dnsmasq

Use this command to test resolution speed:

time dig @127.0.0.1 tbi.dev

For Apache-specific testing, enable debug logging:

LogLevel debug