Virtual Hosts

Providing IP-based virtual hosting is neither contemporary nor difficult. Run multiple AxKit2 servers or put multiple <Server> sections in your axkit.conf. For this reason, it won't be explained in more detail here.

The real challenge is to do name-based virtual hosting, preferrably with the ability to run different server software for each host.

AxKit2 can easily be extended for name-based virtual hosting, that functionality, while not yet fully working, is prepared.

The only way to achieve the second goal, however, is a separate reverse proxy. After you have set up a Squid proxy as shown in ReverseProxy, a simple addition to squid.conf and one perl script adds the desired functionality:

squid.conf:

redirect_program /usr/local/bin/axkit2-redirector.pl

/usr/local/bin/axkit2-redirector.pl:

#!/usr/bin/perl -w
use strict;
$|++;

################## CONFIGURATION ####################
my %domains = qw(
        www.example.com                 site1
        devel.example.com               site2
        internal.example.com            site3
);

#####################################################
# Reload: killall axkit2-redirector.pl
#####################################################

foreach my $udomain (keys %domains) {
        my ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwnam($domains{$udomain});
        open(FH, "<", $dir."/etc/axkit.conf");
        while (<FH>) {
                $domains{$udomain} = ":$1" if m/^\s*port\s+(\d+)\s*$/i;
        }
}

while (<>) {
        my ($prefix, $domain, $port, $local, $ip, $ident, $method) = m{^([a-z0-9]+://(?:.*\.)?)([a-z0-9-]+\.[a-z0-9-]+)((?::[0-9]+)?([^ ]*) ([^ ]+) ([^ ]+) ([^ ]+)\n$}i;
        print, next if (!exists $domains{$domain});
        print $prefix.$domain.$domains{$domain}.$local."\n";
}

This is a low-footprint perl script which maps domains to user ids and reads etc/axkit.conf from the user's home directory to find out what port this user's AxKit2 server is listening at. It is meant to be used with the first scheme of DirectoryLayout.

Note that any URL that is not matched will be served by the main web server as specified in squid.conf.