11

I'm attempting to host multiple website locally with HTTPS enabled, but in order to do this I'd need to configure my local machine to use multiple IP address for localhost (I believe). Is there any way to have multiple IP addresses resolving locally at the same time?

I am using OS X 10.6 (standard edition - not server), and the MAMP server setup.

Thanks!

3 Answers 3

4

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/

8
  • To host multiple sites with HTTPS enabled I believe they each need a separate IP address. See: stackoverflow.com/questions/11217538/… Jun 27, 2012 at 16:01
  • @user1086746 Apache supports TLS Server Name Indication, as do all major client browsers on.. pretty much every OS that's not Windows XP. The answer you should have gotten on your linked question is to set NameVirtualHost *:443. Jun 28, 2012 at 1:01
  • @Shane that still doesn't appear to work when dealing with multiple sites using HTTPS over the same IP address. I did post what ended up working for me. Jun 28, 2012 at 15:12
  • @user1086746 It most certainly works; I'm guessing your NameVirtualHost didn't match your <VirtualHost> blocks, if you were binding specifically to 127.0.0.1. No matter in this case since you've found a working solution, but NoNoNo is right - you don't need extra IPs. Jun 28, 2012 at 15:30
  • Well, I would like to get this working 'correctly' as setting up those IP addresses are an extra step. I have 2 NameVirtualHosts like: NameVirtualHost *:80 NameVirtualHost *:443 and have tried using vhosts like: <VirtualHost _default_:443> and <VirtualHost 127.0.0.1:443>, but neither will provide an HTTPS connection after the first listed vhost. I could provide the full ssl.conf and vhost.conf files if you were interested! Jun 28, 2012 at 15:43
25

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!

2
  • 2
    Using echo PASSWORD is a terrible idea. A crude alternative would be to put something in root's cron like sudo crontab -e and @reboot ifconfig lo0 alias 127.0.0.2. There is certainly a more elegant solution, but anything that involves your password in plain text is unacceptable.
    – chmac
    Apr 10, 2014 at 11:46
  • 2
    Wow, that's a lot of fun! I use this for ssh port forwarding. This allows forwarding the same port multiple times by listening on different local IP addresses. E.g. ssh -L 127.0.0.2:443 foo:443 -L 127.0.0.3:443 beta.foo:443 user@host. You can then define host names to these local IP addresses in your /etc/hosts file if needed. Turns out to be pretty useful for web developing and testing/remoting, especially if you can't use custom ports for your applications.
    – Jules
    Apr 8, 2016 at 19:17
2

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.

3
  • I can't seem to be able to listen without the alias on the loopback interface. Trying nc -vvl 127.0.34.2 9022 returns nc: Can't assign requested address. With the alias defined with ifconfig lo0 alias 127.0.34.2, it does work. Any ideas why that is? Running on OSX Yosemite.
    – Jules
    Aug 6, 2016 at 0:41
  • 1
    In Mac, only 127.0.0.1 is mapped to lo0 (loopback) device. You need to manually add alias to the loopback device to make it work. But in Linux, all 127.0.0.* is mapped to loopback device.
    – Sri
    Jan 10, 2017 at 23:51
  • 2
    For Mac: sudo ifconfig lo0 alias 127.0.0.2 up (and so on, must be done individually for each address)
    – lilalinux
    Jul 12, 2018 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .