Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Using Dnsmasq for local development on OS X (passingcuriosity.com)
46 points by Couto on April 22, 2014 | hide | past | favorite | 38 comments


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.


I wrote - 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.


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 , which has a wrapper for non-rack applications, and can be used to make pow play nice with Procfiles.


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.


For Ruby apps, there's also http://pow.cx/


Instead of symlinking ~/.pow/example to (say) ~/Sites/example, you can `echo 3000 > ~/.pow/example` and example.dev will now point to port 3000.

This lets you boot up your Rails app normally, or via Procfiles, instead of booting up through all the pow.cx magic that can be a real pain to debug.


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.


For pow there is http://anvilformac.com/ if you want a nice gui for managing your sites.

I like to setup our designers with this, but have found it useful myself from time to time.


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.


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.


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...


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


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?


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


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


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


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/ is easier to set up. It'll flush the dns cache everytime its changed.


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.


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)


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/


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


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?


Doesn't work offline, sorry.


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


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...


I use something like this with apache's mod_vhost_alias feature:

http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

I just create a new directory, and with the wildcard DNS, I have a new dev site.


http://dev.sh has a nice auto-setup script for this.


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


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


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


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.


Another neat tool/trick http://xip.io/


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


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.


I think this is really useful when you combine it with vhosts on apache/nginx. On my linux machine I run something similar that allows me to serve static assets, run a local dev server (runserver/flask/unicorn, etc) and have a .dev domain to test with.

You can then combine this with xip.io and be able to share your dev site with others.


I read the article and just thought it's over engenering. I wonder how many different web sites do these people have to configure on their laptops every week.


Maybe I'm missing something, but…

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


/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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: