Programmer Tips‎ > ‎

Public DNS Pointing To localhost (127.0.0.1)

posted Oct 7, 2011, 9:01 AM by Tyler Akins   [ updated Aug 30, 2012, 6:06 AM ]
When you are developing and using a local development environment, you typically need to hit your own site.  A lot.  You'd use URLs that look like this:

http://localhost/
http://127.0.0.1/

When you get slightly more advanced, you would want to run multiple sites off your installation.  You can easily do this with name based virtual hosts (eg. with VirtualHost directives in Apache's config).  Now you want to use urls like this:

http://client1.local/
http://client2.dev/
http://client3/

Those URLs don't work, so now we need to find some way to map our domain names to the "localhost" address.

What if we could map hostnames to 127.0.0.1 and make this work?

Hosts File

The first and easiest method is where one edits their hosts file (/etc/hosts in Linux, C:\Windows\System32\Drivers\etc\hosts for some versions of Windows) and add lines like this:

127.0.0.1 client1.local
127.0.0.1 client2.dev
127.0.0.1 client3

At work, we have up to five different hostnames for each of our clients.  Adding yet another client means dozens of developers that now need to edit their hosts file.  Oh, the pain and agony when you have to do this for hundreds of domains!

What if we could have a single top-level domain that always resolved to localhost?

DNS Entries - Windows

If you are using Windows DNS, you can create a new zone:

dnscmd /RecordAdd local * 3600 A 127.0.0.1
dnscmd /RecordAdd local @ 3600 A 127.0.0.1

dnsmasq - Linux, MacOS

On Linux systems, you can install dnsmasq to pretend to be a real DNS server and actually respond with 127.0.0.1 for all subdomains of a top level domain.  So, if you wanted *.local to always resolve to your own domain, then you can use URLs like this:

http://client1.local/
http://client2.local/
http://client3.local/

You only need to install and set up dnsmasq.  There's some well-written instructions at http://drhevans.com/blog/posts/106-wildcard-subdomains-of-localhost that you can follow; I won't repeat them here.

The drawback of this setup is that you now have to install and configure dnsmasq on every machine where you want to use this trick.

What if someone set up DNS entries and basically did this for you?

Available Wildcarded DNS Domains

It turns out that some kind hearted people already set up wildcarded domains for you already.  You can use any top level domain below and any subdomain of these and they will always resolve back to 127.0.0.1 (your local machine).  Here's the list of ones I know about.  Let me know if there are more!

  • fuf.me - Managed by me; it will always point to localhost for IPv4 and IPv6
  • localtest.me - Also has an SSL cert - see http://readme.localtest.me
  • ulh.us
  • 127-0-0-1.org.uk
  • ratchetlocal.com
  • smackaho.st
  • 42foo.com
  • vcap.me
  • beweb.com
  • yoogle.com
  • ortkut.com
  • feacebook.com
  • lvh.me
Now, with these wildcarded domains, you don't need to do any modification of your system for requests to come back to your own server.  For instance, you can go to http://client1.127-0-0-1.co.uk/ and the web page request will always head back to your own server.  You'll still need to configure your web server to answer on this hostname, but at least the DNS portion of the problem is now solved.
Comments