Jul 6, 2012

OS X Lion and local DNS issues

Many people use their Macs for local development. They have Apache web server running on their computers. This works great. However there is an issue with Mac OS X Lion, which causes DNS issues with ".local" domains. If you use ".local" for development, you may notice that your local Apache seem to answer very slow (5 seconds or so). In fact, it is not Apache, it is DNS lookups that take most of the time. In this article I collected several ways to solve the issue.

The are several reasons for this. Therefore there is no one single solution for all cases. You can to try one or more or even all to find what works for you.


Reasons

One of reasons of these issues is Bonjour: Mac's technology to see computers on the local network. Mac thinks that computers in the ".local" zone are nearby and tries to search for them using Bonjour protocol. When that fails, it goes to other channels. In particular, it will look to your /etc/hosts file only after trying other DNS methods. This is slow.

Another reason, tough unclear, but seems to be related to IPv6 name resolving.

Yet another reason is buggy /etc/hosts support: sometimes OS X seem to ignore multiple entries with the same IP address.

Solutions


Fixing multiple hosts

Make sure that you use a single 127.0.0.1 entry in your /etc/hosts:

127.0.0.1 localhost typo3master.local typo3-45.local

If you had these entries separately, combine them into one

Computer name in /etc/hosts

Make sure you add your computer name to /etc/hosts:

127.0.0.1 mycomputer mycomputer.local localhost typo3master.local typo3-45.local

That resolves the name of your computer if other methods fail.

Fix IPv6

Add entries for all your hosts using IPv6 format in addition to IPv4. Even if you does not use IPv6, that helps:

::1 mycomputer mycomputer.local localhost typo3master.local typo3-45.local
fe80::1%lo0 mycomputer mycomputer.local localhost typo3master.local typo3-45.local

Use dscl instead of /etc/hosts

/etc/hosts is a legacy file in OS X. DNS handling is managed through the dscl utility. Add your hosts like this:

sudo dscl localhost -create /Local/Default/Hosts/typo3master.local IPAddress 127.0.0.1

You need to make that for each of your local domains. This step worked for me best. Others somewhat improved DNS lookups but this made them lightning fast.

Use other zone

If you do not want to make these changes, simply use other DNS suffix (like ".dev" instead of ".local"). I did not try that because I like ".local" more.

Conclusion

OS X is great for development: rock stable, fast, convenient, it has its quirks. This article propose a workaround for one of them.

1 comment:

  1. Doing the development on your local machine has some advantages, like being able to work when you have no reliable Internet connection or simplified debugging.
    But it also has a number of disadvantages. I have seen many cases where someone has developed a website on his local machine and after transferring it to the destination server he figured out that there are a number of problems. These could be caused by different PHP/MySQL versions of settings. Therefore my recommendation is to use the destination server whenever feasible. Also many people noticed that developing on a real internet server rather than local is actually faster (although you need to add all the data transfers). The reason is simple: in most cases the local machines are notebooks and these are really not optimized for server tasks.

    And, as you mentioned: one of the easiest solutions to fix the DNS issue is to use another domain name, not ending in .local

    ReplyDelete