Assigning Multiple IP Addresses to localhost OS X 10.6

Solution 1:

You don't need multiple ip in order to host multiple websites on one web server. You need to use "Virtual Hosts" (with https if you need also it). Here there is a guide for virtual hosts on MAMP http://sawmac.com/mamp/virtual/

Solution 2:

To alias localhost, you can use this terminal command to create a 'loopback':

ifconfig lo0 alias 127.0.0.2

With the localhost alias setup, you can create multiple HTTPS virtual hosts thusly:

<VirtualHost 127.0.0.1:443> ...... </VirtualHost>
<VirtualHost 127.0.0.2:443> ...... </VirtualHost>

You could also initialize these hosts on startup, if desired, through root's cron:

sudo crontab -e
@reboot ifconfig lo0 alias 127.0.0.2

Hopefully this helps anyone running into the same issues I did!

Solution 3:

You never need to add additional IP addresses to the local host on Linux or Windows. They will respond by default, without additional configuration, to all IP addresses from 127.0.0.0/8:

$ ping 127.254.0.100
PING 127.254.0.100 (127.254.0.100) 56(84) bytes of data.
64 bytes from 127.254.0.100: icmp_seq=1 ttl=64 time=0.026 ms

So just make your application listen on any IP from the 127.0.0.0/8 range and you shall be good to go.

Example:

One console:

$ nc -vvl 127.0.34.2 9022
Connection from 127.0.0.1 port 9022 [tcp/*] accepted
Hello

Another console:

$ echo Hello | nc -vv 127.0.34.1 9022
nc: connect to 127.0.34.1 port 9022 (tcp) failed: Connection refused
$ echo Hello | nc -vv 127.0.34.2 9022
Connection to 127.0.34.2 9022 port [tcp/*] succeeded!

As a commenter noted, it is necessary to add IPs to the localhost interface explicitly on MacOS/Darwin.