13.04.10

Easy DNS wildcard setup for local domains using dnsmasq

When doing web development you'll often have domains/hostnames like hahler.local (instead of hahler.de).

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:

Code:

address=/local/127.0.0.1

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):

Code:

hosts:          files dns mdns4_minimal [NOTFOUND=return] mdns4
By Daniel in Ubuntu, Snippets04/13/10 English (US) Email

7 comments

Comment from: Robin Perkins [Visitor]
Robin PerkinsHey dude,

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.
05/03/10 @ 19:29
Comment from: Jeremy Kendall [Visitor] Email
Jeremy KendallThis is exactly what I'd like to do, but I can't get the setup running properly.

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?
05/15/10 @ 22:48
Comment from: Wim Koorenneef [Visitor]
Wim KoorenneefThis also works great in my Linksys router running DD_WRT. Thanks!
05/18/10 @ 08:57
Comment from: Daniel [Member] Email
@Jeremy: this sounds like it's an Apache setup issue - after all, the request arrives at Apache. Are you being fooled by a previous "permanent" redirect or something alike?
Try "curl -L -I foo.example.dev" to see what happens.
05/19/10 @ 08:46
Comment from: Callum [Visitor]
CallumThe "prepend domain-name-servers 127.0.0.1;" in /etc/dhcp3/dhclient.conf is what makes it truly beautiful.

Plus, dnsmasq will cache dns requests, which seems like a good thing somehow... :-)
06/15/10 @ 00:22
Comment from: Samuel Kadolph [Visitor]
Samuel KadolphNot sure what distros this will apply for but I was trying to get this to work on Ubuntu 10.04.1 and couldn't get it to work. dnsmasq wasn't being asked to resolve *.local.
I fixed this by using another domain (i.e. *.dev).
10/07/10 @ 23:46
Comment from: Wallysson [Visitor]
Wallysson@Jeremy - Or how had same issue, access *.dev but can't access *.example.dev
to fix it, you just need to put 'ServerAlias *.dev' inside your virtual host, and everything should work properly... :)
11/23/11 @ 13:46

Leave a comment


Your email address will not be revealed on this site.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
This is a captcha-picture. It is used to prevent mass-access by robots.
Please enter the characters from the image above. (case insensitive)
You can just use your OpenID to provide your name, e-mail and url.
Seitenleiste