Easy DNS wildcard setup for local domains using dnsmasq
When doing web development you'll often have domains/hostnames like
hahler.local (instead of
Having a separate hostname for a lot of projects will result in some inconvenience though: you have to setup those.
Until recently, I was adding them to /etc/hosts (or the counterpart on Windows), like so:
127.0.0.1 hahler.local whissip.local
This however does not allow wildcards (like
*.hahler.local), and having something like
127.0.0.1 *.local would be much easier to maintain (no adding to this file for a new setup/project).
dnsmasq to the rescue!
dnsmasq is a lightweight DNS forwarder and I was using it already to cache DNS queries locally, for faster browsing etc. Since it answers my DNS lookups already, it should be able to make *.local work for me.
I've created a simple file
/etc/dnsmasq.d/my-local with just a single line:
And after restarting dnsmasq, *.local gets resolved to 127.0.0.1.
Instead of "local" you can (and should probably) use another domain, like "dev" instead, because using "local" conflicts with Avahi/mdns, but this can get solved as follows:
Avahi/mdns defaults to ".local" as its TLD and therefore you have to adjust the "hosts" line in /etc/nsswitch.conf and put "dns" (which is dnsmasq in this case) before mdns4_minimal (which is avahi in this case), or remove the NOTFOUND=return, so that "dns" would get used even when mdns4_minimal returns NOTFOUND (which it apparently does for requests in its domain (typically ".local") it does not know anything about):
I would be vary cautious moving the /etc/nsswitch.conf order around. By putting dns before mdns you may get your local webserver working but your polluting the unicast dns space up with multicast dns .local lookups (and I guess breaking Avahi/Zeroconf in the process). The .local top level domain is essentially reserved - avoid using it like the plague for anything other than mdns.
Have you considered using the Apache Avahi plugin (I'm assuming your using Apache)- that way all the hosts on your local network will be able to see and browse the test websites. For example on a windows host you can download Bonjour for Windows from Apple and all of your virtual websites will appear in the IE bookmarks menu - handy for testing multiple browsers without the need type / remember all the URLs for each project.
Steps taken so far:
* Installed dnsmasq
* Set address=/example.dev/127.0.0.1 in dnsmasq.conf
* Set listen-address=127.0.0.1 in dnsmasq.conf
* Ensured nameserver 127.0.0.1 is in /etc/resolv.conf
* Set prepend domain-name-servers 127.0.0.1; in /etc/dhcp3/dhclient.conf
* Created a vhost for example.dev (includes ServerAlias *.example.dev)
* Restarted apache and dnsmasq
Note: example.dev is not set in /etc/hosts.
The setup will serve example.dev locally without any problem. It will also serve *.example.dev, but *.example.dev returns the default apache "It works!" index.html from /var/www rather than my index.php in /home/jkendall/public_html/example/public.
For grins, I moved my project over to /var/www/example and modified the vhost appropriately. I got the same result as described above.
At this point I'm not sure what other steps I can take. Thoughts?
Try "curl -L -I foo.example.dev" to see what happens.
Plus, dnsmasq will cache dns requests, which seems like a good thing somehow... :-)
I fixed this by using another domain (i.e. *.dev).
to fix it, you just need to put 'ServerAlias *.dev' inside your virtual host, and everything should work properly... :)