Apache Virtual Host Configuration
Image via Wikipedia
Apache: Name Based Virtual Host
The server machine has a primary name server.domain.tld. Then there are two aliases (CNAMEs) for that server say www.domain.tld and www.sub.domain.tld for the address server.domain.tld.
We need different server configuration for each name so that each name can act as differrent web servers.
Here is the configuration for the vhost.conf file (the one that will be included in the main config file httpd.conf)
ServerName server.domain.tld
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /www/domain
ServerName www.domain.tld
…
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /www/subdomain
ServerName www.sub.domain.tld
…
</VirtualHost>
The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.domain.tld is first in the configuration file, it has the highest priority and can be seen as the default or primary server.
Apache: Config for IP-based virtual hosts
Setup 1
The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www.otherdomain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server.
Here is the configuration for the vhost.conf file (the one that will be included in the main config file httpd.conf)
DocumentRoot /www/domain
ServerName www.domain.tld
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
…
</VirtualHost>
www.otherdomain.tld can only be reached through the address 111.22.33.55, while www.domain.tld can only be reached through 111.22.33.44 (which represents our main server).
Setup 2
Same as setup 1, but we don’t want to have a dedicated main server.
Here is the configuration for the vhost.conf file (the one that will be included in the main config file httpd.conf)
Port 80
ServerName server.domain.tld
<VirtualHost 111.22.33.44>
DocumentRoot /www/domain
ServerName www.domain.tld
…
</VirtualHost>
<VirtualHost 111.22.33.55>
DocumentRoot /www/otherdomain
ServerName www.otherdomain.tld
…
</VirtualHost>
The main server can never catch a request, because all IP addresses of our machine are in use for IP-based virtual hosts (only localhost requests can hit the main server).
Setup 3:
The server machine has two IP addresses (111.22.33.44 and 111.22.33.55) which resolve to the names server.domain.tld and www-cache.domain.tld respectively. The hostname www.domain.tld is an alias (CNAME) for server.domain.tld and will represent the main server. www-cache.domain.tld will become our proxy-cache listening on port 8080, while the web server itself uses the default port 80.
Here is the configuration for the vhost.conf file (the one that will be included in the main config file httpd.conf)
Listen 111.22.33.44:80
Listen 111.22.33.55:8080
ServerName server.domain.tld
<VirtualHost 111.22.33.44:80>
DocumentRoot /www/domain
ServerName www.domain.tld
…
</VirtualHost>
<VirtualHost 111.22.33.55:8080>
ServerName www-cache.domain.tld
…
<Directory proxy:>
Order Deny,Allow
Deny from all
Allow from 111.22.33
</Directory>
</VirtualHost>
The main server can never catch a request, because all IP addresses (apart from localhost) of our machine are in use for IP-based virtual hosts. The web server can only be reached on the first address through port 80 and the proxy only on the second address through port 8080.
Port-based vhosts
The server machine has one IP address (111.22.33.44) which resolves to the name www.domain.tld. If we don’t have the option to get another address or alias for our server we can use port-based vhosts if we need a virtual host with a different configuration.
Here is the configuration for the vhost.conf file (the one that will be included in the main config file httpd.conf)
Listen 8080
ServerName www.domain.tld
DocumentRoot /www/domain
<VirtualHost 111.22.33.44:8080>
DocumentRoot /www/domain2
…
</VirtualHost>
A request to www.domain.tld on port 80 is served from the main server and a request to port 8080 is served from the virtual host.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=b48896d5-6b72-4824-a403-e338c7dbca8c)




Recent Comments