B.6. Configuring Apache for Multiple Servers on One Public IP

If you only have one public IP but run multiple web servers, you can set up the others on other port numbers. However giving out URL's like http://www.example.com:81 isn't exactly ideal. You're bound to have people trying to get to http://www.example.com, and since your port 80 points to another web server, the person will get the wrong web page.

You can get around this by using name-based virtual hosting on the web server on port 80. This configuration will work with any web server that supports name-based virtual hosting (most any does), but this section will describe how to configure Apache for this purpose.

For this configuration, port 80 is www.example.com, port 81 is www.whatever.com and port 82 is www.example.net. These are three separate physical web servers.

At the bottom of your httpd.conf (in /usr/local/etc/apache/ in FreeBSD, the location of your configuration file may vary) add the following lines. This is on the server that is accessed via port 80 from the internet.

    NameVirtualHost 192.168.1.12

    <VirtualHost 192.168.1.12>
        UseCanonicalName off
        ServerName www.example.com
        DocumentRoot /usr/local/www/data/
    </VirtualHost>

    <VirtualHost 192.168.1.12>
        UseCanonicalName off
        ServerName www.whatever.com
        Redirect / http://www.whatever.com:81
    </VirtualHost>
    
    <VirtualHost 192.168.1.12>
        UseCanonicalName off
        ServerName www.example.net
        Redirect / http://www.example.net:82
    </VirtualHost>

That configuration will keep www.example.com local, with the site's files in /usr/local/www/data/, and will redirect any requests to www.whatever.com to www.whatever.com:81 and www.example.net to www.example.net:82.

It's not an ideal setup, but if you're stuck with multiple web servers and a single public IP to reference all of them, it's better than people getting the wrong page when forgetting to put the port after the URL.