Multiple web servers behind one public IP

Solution 1:

Set up one more vm as proxy, that would direct traffic to appropriate vm using domain name.

Solution 2:

I have tried various solutions and the success I have is using Apache2. It comes with virtual hosting feature.

You will need to do a little "wiring" the Apache2 but, I am including my conf as a guide for you. How this works: apache2 operates on "Name-Based Domain" feature where it runs as many sites as I want but on one public IP. [I am a developer which explains why these many sites].

   $ sudo apt-get install -y apache2 apache2-dev apache2-threaded-dev

Wiring your apache2.conf by adding these three lines in the end of the conf

   include httpd.conf
   <VirtualHost *>
   </VirtualHost>

Configure your httpd.conf with this guide; adjust accordingly to your case. By the default as new installation, you will not see httpd.conf but Apache2 will respond if it finds httpd.conf sitting in /etc/apache2 directory.

    $ sudo touch /etc/apache2/httpd.conf

and add this to httpd.conf. Big emphasis is to leave :80 untouched. When you see '## EDIT THIS*'; that's where you can edit to your own liking meanwhile leaving others untouched.

   <Directory "/var/www">  ***## EDIT THIS***
      Options Indexes FollowSymLinks
      AllowOverride All
   </Directory>

    <location /cgi-bin>
    AddHandler cgi-script .cgi .pl
    Options -Indexes +FollowSymlinks +ExecCGI
    </location>


  <VirtualHost *:80>
    ServerName example.ca  ***## EDIT THIS***
    ServerAlias www.example.ca ***## EDIT THIS***
    DocumentRoot /var/www/example_ca/html ***## EDIT THIS***
    <Location "/">
            Order Deny,Allow
            Deny from all
            Allow from all
    </Location>
      CustomLog /path/to/your/preferred/log-folder/access.log common ***## EDIT THIS***
      ErrorLog /path/to/your/preferred/log-folder/error.log ***## EDIT THIS***
   </VirtualHost>

  <VirtualHost *:80>
    ServerName sample.ca ***## EDIT THIS***
    ServerAlias www.sample.ca ***## EDIT THIS***
    DocumentRoot /var/www/sample_ca/html ***## EDIT THIS***
    <Location "/">
            Order Deny,Allow
            Deny from all
            Allow from all
    </Location>
      CustomLog /path/to/your/preferred/log-folder/access.log common ***## EDIT THIS***
      ErrorLog /path/to/your/preferred/log-folder/error.log ***## EDIT THIS***
   </VirtualHost>

then...

   $ sudo service apache2 restart

and voila! Good luck!

EDIT: Keep in mind - after setting up sites and tested to be operational on your local network, you need to update DNS with your DNS service provider (where you registered these domains) and update your ip address (where you are hosting your machine with these multi-sites).

Solution 3:

The router does not allow port forwarding to multiple IP's using the same port (80).

This isn't possible with standard TCP/IP, by the way. You need a program, i.e. a proxy, accepting requests on the port, looking at the requests, then shunting requests around accordingly.


Apache and other web servers have a "reverse proxy" feature that can be used to "split" your incoming traffic. A reverse proxy tells Apache that requests for a certain web path, i.e. "yourdomain/directory1" should be thrown to another webserver, and the responses from that webserver thrown back to the original client.

Beware, however, that if you have applications running on this webserver (PHP, etc.) that they may need to have settings changed as their externally facing URL will be different. You may have to tell a PHP application that it lives at "yourdomain/directory1" instead of "yourdomain" so it can generate links correctly. It's possible to tell Apache to rewrite traffic from the backend server before it gives it back to the client but this is difficult and you want to avoid it if possible. Applications that generate links with Javascript can be especially problematic, so not all web applications are easily "reverse proxyable."

So install Apache on a fourth "front end" VM - forward your incoming TCP 80 to this VM. You also need to make sure this VM can reach your other VMs on your "virtual network." Then, on your "front end" VM, you can set up a separate reverse proxy for each website.

Here is the relevant Apache configuration details, using the ProxyPass and ProxyPassReverse directives.

You can also get elaborate and implement caching, which would help performance.