Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

This question already has an answer here:

I am trying to run multiple Node.js web servers locally on my machine.

Because the code I'm writing needs to reference a domain name I'd like to keep a convention of dev.myHost.com and point that to the non-port-80 service running from Node (technically I'm running one on Harp.js and one on Sails.js).

  • So dev.hostOne.com should point to localhost:123
  • and dev.hostTwo.com should point to localhost:456

Is this possible? If so, how.

share|improve this question

marked as duplicate by Sven, Ward, Falcon Momot, Michael Hampton Feb 9 '14 at 18:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Can we remove the "duplicate" flag? The others don't offer a working solution. I hope everyone hitting this one at least scrolls down to see it worked. –  mondo Feb 10 '14 at 22:18
The reason why there's no working solution in the other question is because there is no working solution, because that is just not how DNS works. –  Jenny D Sep 23 '14 at 8:16
@JennyD Scroll down and see the answer from KrisFR does indeed work. –  mondo Sep 23 '14 at 13:27

1 Answer 1

up vote 4 down vote accepted

From my understanding, it seems that you only use one local dev machine, means not connected to any network that provides a DNS.

If i am right, i would suggest to setup things like described below :

step 1 : Assign at least two IP address to your MAC OS (one per domain), let's say :

To setup the second IP you will have to add a second Ethernet Adapter (logical not physical).

Step 2 : As you don't have a DNS server, you could setup your /etc/hosts file, by adding :     dev.hostone.com     dev.hosttwo.com

Step 3 : Assign aliases to your loopback interface :

sudo ifconfig lo0 alias
sudo ifconfig lo0 alias

Step 4 : Setup ipfw to forward packets :

sudo ipfw add fwd,123 tcp from me to dst-port 80
sudo ipfw add fwd,456 tcp from me to dst-port 80

You are done !

Now :

enter image description here enter image description here

I've setup two Node.js web servers to test your case :

$ netstat -anp tcp | grep -E "123|456"
tcp4    0    0    *.*    LISTEN
tcp4    0    0    *.*    LISTEN

Important : note that ipfw rules and loopback interface aliases are not persistent and will no longer exist after a reboot. So consider adding a startup script.

share|improve this answer
Worked BEAUTIFULLY. This is EXACTLY what I wanted. –  mondo Feb 10 '14 at 15:19
Glad it works ! i was pretty sure that it was not totally a "duplicate" ;) –  krisFR Feb 10 '14 at 15:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.