

Using Dnsmasq for local development on OS X - Couto
http://passingcuriosity.com/2013/dnsmasq-dev-osx/

======
sexmonad
I use "lvh.me" \- it has a wildcard DNS record to redirect any subdomain to
localhost. So I'll use something like "example.com.lvh.me" to test.

------
gnufied
I wrote - [http://invoker.codemancers.com/](http://invoker.codemancers.com/)
for similar use. It provides easy to use `.dev` local domain support and then
ability to manage multiple processes via a Procfile or custom ini file.

~~~
rcsorensen
Thanks for this. Procfile support has been a thorn in my side with pow for
ages.

Edit: Invoker doesn't seem to support the pow use case -- local configuration,
not global. Inspired by the topic, I found
[https://github.com/riywo/anypow](https://github.com/riywo/anypow) , which has
a wrapper for non-rack applications, and can be used to make pow play nice
with Procfiles.

------
ironlady
I've been doing this for about a year or so now and its honestly the best
solution for local development I've used in my career.

Recently I've started using Vagrant for more complex sites that require custom
server software, or are based outside the traditional LAMP stack, but a
Dnsmasq based setup on my mac is still my favorite for most sites.

------
javan
For Ruby apps, there's also [http://pow.cx/](http://pow.cx/)

~~~
e28eta
I really enjoy using Pow for non-Ruby apps too.

It handles the .dev DNS resolution, and also supports hosting domains that you
map through /etc/hosts (ex: foo.company.com if you need to share cookies with
a SSO server, but don't want all of company.com directed to your local
machine)

It is much easier to configure than Apache vhosts for distributing traffic
based on host name:

* symlink to Rack app for Ruby support. Name of symlink becomes hostname

* create a directory and symlink Public for static HTML. Directory name is hostname

* as sibling said, put port # in text file to proxy traffic to another process. File name is hostname

I use Anvil for Mac to do some of my Pow management.

I'm also using Apache to do SSL termination and proxy to Pow, as well as host
some other stuff. But Pow is so easy to use for both static sites and proxying
to another web server, I prefer it.

------
iambyteman
I've been experimenting with this lately. I have been using dnsmasq to server
up domains in my /etc/hosts file to local virtual machines for testing on
different platforms without having to mess around with the hosts file in each
virtual machine.

I am about to loop back around to a mobile project and I am hoping I can use
it to tests my mobile app interactions with my local development box more
easily than I have been able to in the past.

------
Spittie
Another neat "trick" with dnsmasq is to redirect all the dns request for your
domain to your dns provider when you're working with dns records (config
needed: server=/example.com/176.124.112.100). Never wait for dns propagation
anymore! Beat switching your system's dns to your provider because those could
be slow for you, or could not answer domains that aren't in their database.

------
magnetikonline
I do something similar with Ubuntu 12.04/14.04 which uses Dnsmasq via the
NetworkManager service. These steps help make Dnsmasq a little more useful -
reading /etc/hosts and allowing it to listen on your hosts IP address for use
in guest VM's

[https://gist.github.com/magnetikonline/6236150](https://gist.github.com/magnetikonline/6236150)

------
josephby
Or you could use ettercap to redirect all DNS requests for any device on your
local network. That's how I test iPhone, iPad, and Android applications when
the services needed don't have DNS entries:
[http://joseph.by/post/83414951259/effectively-edit-hosts-
fil...](http://joseph.by/post/83414951259/effectively-edit-hosts-file-with-
ettercap-on-mac)

------
joshka
Great article for highlighting the dnsmasq on OSX idea. I wasn't aware that
you could run this there.

On the goal of having unlimited easy to configure real domains that point to
locahost, another service of interst is localtest.me [1]. This offers
*.localtest.me -> 127.0.0.1

[1]: [http://readme.localtest.me/](http://readme.localtest.me/)

~~~
thomasjonas
This is pretty useful! Maybe a noob question but how does this work offline /
slow internet connections? I'm coding on the train pretty frequently and I
hate waiting for urls to resolve... I'm guessing it's caching this DNS entry?
(why would I otherwise have to run dscacheutil -flushcache...)

~~~
joshka
It has a 1 hour TTL, so assuming you start working before you go offline you
might be ok. Fair point though. It could be worth upping that to a day?

------
joevandyk
One problem I've been running into: I have a virtual machine (linux running in
vmware fusion on osx). I have dev.site.com in /etc/hosts. The vm is set to use
a private network on the vm (as I don't want the ip address to change when I
switch networks). How can I setup my phone to load dev.site.com and have it
work while keeping the private network setup?

~~~
stock_toaster
run an http proxy on your host os and have the phone configured to use that?

~~~
joevandyk
how does the iphone know to route requests for the private network to the host
os?

~~~
jasomill
HTTP Proxy in iOS Settings offers three options:

 _Off:_ proxies no HTTP requests.

 _Manual:_ proxies all HTTP requests through a proxy server at the specified
host and port.

 _Auto:_ uses JavaScript code hosted on a Web server at a specified
(unproxied) URL to determine how HTTP requests get proxied. Assuming your
proxy server is at host.example.com:8080, specifying a URL pointing to a file
containing

    
    
        function FindProxyForURL(url, host) {
            if (shExpMatch(host, "*.vm.example.com")) {
                return "PROXY host.example.com:8080";
            } else {
                return "DIRECT";
            }
        }
    

should proxy only HTTP requests whose URLs point to hosts in the
.vm.example.com subdomain through your proxy server.

Disclaimer: I haven't tested any of this, and everything I know about proxy
auto-configuration comes from skimming the Wikipedia page[1] just now.

[1] [http://en.wikipedia.org/wiki/Proxy_auto-
config](http://en.wikipedia.org/wiki/Proxy_auto-config)

------
arb99
Doesn't do the exact same (you'd have to add a new entry everytime, rather
than point *.dev to localhost) but [https://addons.mozilla.org/en-
US/firefox/addon/hostadmin/](https://addons.mozilla.org/en-
US/firefox/addon/hostadmin/) is easier to set up. It'll flush the dns cache
everytime its changed.

~~~
tvon
No, you don't have to add a new entry every time, you just set it up and now
absolutely.anything.dev points to localhost. When editing /etc/hosts you have
to add a line for every hostname.

~~~
arb99
yeah thats what i meant and said.

by using something to edit the hosts file you'd have to edit it every time
(can't do wildcards on hosts file, so it needs editing every time)

------
eswat
Well, that’s one part of the equation. Are there any guides on what to do
after; setting up virtual hosts?

~~~
flexd
I use nginx, which is a great alternative to Apache.

I've never tried it on OSX, but this guide seems to cover dnsmasq + nginx
virtualhosts :-)

[http://zaiste.net/2013/03/serving_apps_locally_with_nginx_an...](http://zaiste.net/2013/03/serving_apps_locally_with_nginx_and_pretty_domains/)

------
raamdev
[http://dev.sh](http://dev.sh) has a nice auto-setup script for this.

------
dshankar
Genuinely curious: when & why would I want to use myproject.dev:port over
localhost:port?

~~~
caw
You can't do subdomain.localhost:port, so the idea is to create a DNS server
that will resolve .dev as a TLD to 127.0.0.1

------
aalvarado
I wrote a little post for myself on the same topic, detailing how to get this
working on Linux (Ubuntu & variants).
[https://coderwall.com/p/6dgpsw](https://coderwall.com/p/6dgpsw)

------
freshyill
I use virtualhost.sh, which is available via homebrew. Maybe the use case is a
bit different, but it seems a heck of a lot easier for most uses.

------
r4um
Another neat tool/trick [http://xip.io/](http://xip.io/)

------
etchalon
Maybe I'm missing something, but…

…how is this different from editing /etc/hosts?

~~~
Xylakant
/etc/hosts offers no wildcard support. You can teach dnsmasq to route
everything under the .dev TLD to localhost, so you can effectively create
hostnames (including subdomains) by just using them.

------
taylorbuley
What are the advantages of using an approach like this vs. editing /etc/hosts
directly?

~~~
bemurphy
Two immediate I can think of:

1) You don't have to add new .dev hosts to /etc/hosts repeatedly. Once you're
setup, all .dev is local

2) /etc/hosts doesn't support wildcards, so dnsmasq is great with apps that
have many or randomized subdomains.

