<code>hosts</code> entries.</p>
</note>
- <example>
- <title>Server configuration</title>
-
- # Ensure that Apache listens on port 80<br />
- Listen 80<br />
- <br />
- <br />
- <VirtualHost *:80><br />
- <indent>
- DocumentRoot /www/example1<br />
- ServerName www.example.com<br />
- <br />
- # Other directives here<br />
- <br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost *:80><br />
- <indent>
- DocumentRoot /www/example2<br />
- ServerName www.example.org<br />
- <br />
- # Other directives here<br />
- <br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+# Ensure that Apache listens on port 80
+Listen 80
+<VirtualHost *:80>
+ DocumentRoot /www/example1
+ ServerName www.example.com
+
+ # Other directives here
+</VirtualHost>
+
+<VirtualHost *:80>
+ DocumentRoot /www/example2
+ ServerName www.example.org
+
+ # Other directives here
+</VirtualHost>
+ </highlight>
<p>The asterisks match all addresses, so the main server serves no
requests. Due to the fact that the virtual host with
will serve the "main" server, <code>server.example.com</code> and on the
other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 80<br />
- <br />
- # This is the "main" server running on 172.20.30.40<br />
- ServerName server.example.com<br />
- DocumentRoot /www/mainserver<br />
- <br />
- <VirtualHost 172.20.30.50><br />
- <indent>
- DocumentRoot /www/example1<br />
- ServerName www.example.com<br />
- <br />
- # Other directives here ...<br />
- <br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.50><br />
- <indent>
- DocumentRoot /www/example2<br />
- ServerName www.example.org<br />
- <br />
- # Other directives here ...<br />
- <br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 80
+
+# This is the "main" server running on 172.20.30.40
+ServerName server.example.com
+DocumentRoot /www/mainserver
+
+<VirtualHost 172.20.30.50>
+ DocumentRoot /www/example1
+ ServerName www.example.com
+
+ # Other directives here ...
+</VirtualHost>
+
+<VirtualHost 172.20.30.50>
+ DocumentRoot /www/example2
+ ServerName www.example.org
+
+ # Other directives here ...
+</VirtualHost>
+ </highlight>
<p>Any request to an address other than <code>172.20.30.50</code> will be
served from the main server. A request to <code>172.20.30.50</code> with an
with the same content, with just one <code>VirtualHost</code>
section.</p>
- <example>
- <title>Server configuration</title>
-
- <br />
- <VirtualHost 192.168.1.1 172.20.30.40><br />
- <indent>
- DocumentRoot /www/server1<br />
- ServerName server.example.com<br />
- ServerAlias server<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost 192.168.1.1 172.20.30.40>
+ DocumentRoot /www/server1
+ ServerName server.example.com
+ ServerAlias server
+</VirtualHost>
+ </highlight>
<p>Now requests from both networks will be served from the same
<code>VirtualHost</code>.</p>
takes place after the best matching IP address and port combination
is determined.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 80<br />
- Listen 8080<br />
- <br />
- <VirtualHost 172.20.30.40:80><br />
- <indent>
- ServerName www.example.com<br />
- DocumentRoot /www/domain-80<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40:8080><br />
- <indent>
- ServerName www.example.com<br />
- DocumentRoot /www/domain-8080<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40:80><br />
- <indent>
- ServerName www.example.org<br />
- DocumentRoot /www/otherdomain-80<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40:8080><br />
- <indent>
- ServerName www.example.org<br />
- DocumentRoot /www/otherdomain-8080<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 80
+Listen 8080
+
+<VirtualHost 172.20.30.40:80>
+ ServerName www.example.com
+ DocumentRoot /www/domain-80
+</VirtualHost>
+
+<VirtualHost 172.20.30.40:8080>
+ ServerName www.example.com
+ DocumentRoot /www/domain-8080
+</VirtualHost>
+
+<VirtualHost 172.20.30.40:80>
+ ServerName www.example.org
+ DocumentRoot /www/otherdomain-80
+</VirtualHost>
+
+<VirtualHost 172.20.30.40:8080>
+ ServerName www.example.org
+ DocumentRoot /www/otherdomain-8080
+</VirtualHost>
+ </highlight>
</section>
<code>www.example.com</code> and <code>www.example.org</code>
respectively.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 80<br />
- <br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/example1<br />
- ServerName www.example.com<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.50><br />
- <indent>
- DocumentRoot /www/example2<br />
- ServerName www.example.org<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 80
+
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/example1
+ ServerName www.example.com
+</VirtualHost>
+
+<VirtualHost 172.20.30.50>
+ DocumentRoot /www/example2
+ ServerName www.example.org
+</VirtualHost>
+ </highlight>
<p>Requests for any address not specified in one of the
<code><VirtualHost></code> directives (such as
respectively. In each case, we want to run hosts on ports 80 and
8080.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 172.20.30.40:80<br />
- Listen 172.20.30.40:8080<br />
- Listen 172.20.30.50:80<br />
- Listen 172.20.30.50:8080<br />
- <br />
- <VirtualHost 172.20.30.40:80><br />
- <indent>
- DocumentRoot /www/example1-80<br />
- ServerName www.example.com<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40:8080><br />
- <indent>
- DocumentRoot /www/example1-8080<br />
- ServerName www.example.com<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.50:80><br />
- <indent>
- DocumentRoot /www/example2-80<br />
- ServerName www.example.org<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.50:8080><br />
- <indent>
- DocumentRoot /www/example2-8080<br />
- ServerName www.example.org<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 172.20.30.40:80
+Listen 172.20.30.40:8080
+Listen 172.20.30.50:80
+Listen 172.20.30.50:8080
+
+<VirtualHost 172.20.30.40:80>
+ DocumentRoot /www/example1-80
+ ServerName www.example.com
+</VirtualHost>
+
+<VirtualHost 172.20.30.40:8080>
+ DocumentRoot /www/example1-8080
+ ServerName www.example.com
+</VirtualHost>
+
+<VirtualHost 172.20.30.50:80>
+ DocumentRoot /www/example2-80
+ ServerName www.example.org
+</VirtualHost>
+
+<VirtualHost 172.20.30.50:8080>
+ DocumentRoot /www/example2-8080
+ ServerName www.example.org
+</VirtualHost>
+ </highlight>
</section>
<p>Any address mentioned in the argument to a virtualhost that never
appears in another virtual host is a strictly IP-based virtual host.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 80<br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/example1<br />
- ServerName www.example.com<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/example2<br />
- ServerName www.example.org<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/example3<br />
- ServerName www.example.net<br />
- </indent>
- </VirtualHost><br />
- <br />
- # IP-based<br />
- <VirtualHost 172.20.30.50><br />
- <indent>
- DocumentRoot /www/example4<br />
- ServerName www.example.edu<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.60><br />
- <indent>
- DocumentRoot /www/example5<br />
- ServerName www.example.gov<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 80
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/example1
+ ServerName www.example.com
+</VirtualHost>
+
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/example2
+ ServerName www.example.org
+</VirtualHost>
+
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/example3
+ ServerName www.example.net
+</VirtualHost>
+
+# IP-based
+<VirtualHost 172.20.30.50>
+ DocumentRoot /www/example4
+ ServerName www.example.edu
+</VirtualHost>
+
+<VirtualHost 172.20.30.60>
+ DocumentRoot /www/example5
+ ServerName www.example.gov
+</VirtualHost>
+ </highlight>
</section>
used so that the desired hostname is passed through, in case we are
proxying multiple hostnames to a single machine.</p>
- <example>
- <VirtualHost *:*><br />
- ProxyPreserveHost On<br />
- ProxyPass / http://192.168.111.2/<br />
- ProxyPassReverse / http://192.168.111.2/<br />
- ServerName hostname.example.com<br />
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost *:*>
+ ProxyPreserveHost On
+ ProxyPass / http://192.168.111.2/
+ ProxyPassReverse / http://192.168.111.2/
+ ServerName hostname.example.com
+</VirtualHost>
+ </highlight>
</section>
port, <em>i.e.</em>, an address/port combination that is not used for
any other virtual host.</p>
- <example>
- <title>Server configuration</title>
-
- <VirtualHost _default_:*><br />
- <indent>
- DocumentRoot /www/default<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost _default_:*>
+ DocumentRoot /www/default
+</VirtualHost>
+ </highlight>
<p>Using such a default vhost with a wildcard port effectively prevents
any request going to the main server.</p>
<p>Same as setup 1, but the server listens on several ports and we want
to use a second <code>_default_</code> vhost for port 80.</p>
- <example>
- <title>Server configuration</title>
-
- <VirtualHost _default_:80><br />
- <indent>
- DocumentRoot /www/default80<br />
- # ...<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost _default_:*><br />
- <indent>
- DocumentRoot /www/default<br />
- # ...<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost _default_:80>
+ DocumentRoot /www/default80
+ # ...
+</VirtualHost>
+
+<VirtualHost _default_:*>
+ DocumentRoot /www/default
+ # ...
+</VirtualHost>
+ </highlight>
<p>The default vhost for port 80 (which <em>must</em> appear before any
default vhost with a wildcard port) catches all requests that were sent
<p>We want to have a default vhost for port 80, but no other default
vhosts.</p>
- <example>
- <title>Server configuration</title>
-
- <VirtualHost _default_:80><br />
- DocumentRoot /www/default<br />
- ...<br />
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost _default_:80>
+DocumentRoot /www/default
+...
+</VirtualHost>
+ </highlight>
<p>A request to an unspecified address on port 80 is served from the
default vhost. Any other request to an unspecified address and port is
(<code>172.20.30.50</code>) to the <code>VirtualHost</code>
directive.</p>
- <example>
- <title>Server configuration</title>
-
- Listen 80<br />
- ServerName www.example.com<br />
- DocumentRoot /www/example1<br />
- <br />
- <VirtualHost 172.20.30.40 172.20.30.50><br />
- <indent>
- DocumentRoot /www/example2<br />
- ServerName www.example.org<br />
- # ...<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/example3<br />
- ServerName www.example.net<br />
- ServerAlias *.example.net<br />
- # ...<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+Listen 80
+ServerName www.example.com
+DocumentRoot /www/example1
+
+<VirtualHost 172.20.30.40 172.20.30.50>
+ DocumentRoot /www/example2
+ ServerName www.example.org
+ # ...
+</VirtualHost>
+
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/example3
+ ServerName www.example.net
+ ServerAlias *.example.net
+ # ...
+</VirtualHost>
+ </highlight>
<p>The vhost can now be accessed through the new address (as an
IP-based vhost) and through the old address (as a name-based
containing links with an URL prefix to the name-based virtual
hosts.</p>
- <example>
- <title>Server configuration</title>
-
- <VirtualHost 172.20.30.40><br />
- <indent>
- # primary vhost<br />
- DocumentRoot /www/subdomain<br />
- RewriteEngine On<br />
- RewriteRule . /www/subdomain/index.html<br />
- # ...<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40><br />
- DocumentRoot /www/subdomain/sub1<br />
- <indent>
- ServerName www.sub1.domain.tld<br />
- ServerPath /sub1/<br />
- RewriteEngine On<br />
- RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
- # ...<br />
- </indent>
- </VirtualHost><br />
- <br />
- <VirtualHost 172.20.30.40><br />
- <indent>
- DocumentRoot /www/subdomain/sub2<br />
- ServerName www.sub2.domain.tld<br />
- ServerPath /sub2/<br />
- RewriteEngine On<br />
- RewriteRule ^(/sub2/.*) /www/subdomain$1<br />
- # ...<br />
- </indent>
- </VirtualHost>
- </example>
+ <highlight language="config">
+<VirtualHost 172.20.30.40>
+ # primary vhost
+ DocumentRoot /www/subdomain
+ RewriteEngine On
+ RewriteRule . /www/subdomain/index.html
+ # ...
+</VirtualHost>
+
+<VirtualHost 172.20.30.40>
+DocumentRoot /www/subdomain/sub1
+ ServerName www.sub1.domain.tld
+ ServerPath /sub1/
+ RewriteEngine On
+ RewriteRule ^(/sub1/.*) /www/subdomain$1
+ # ...
+</VirtualHost>
+
+<VirtualHost 172.20.30.40>
+ DocumentRoot /www/subdomain/sub2
+ ServerName www.sub2.domain.tld
+ ServerPath /sub2/
+ RewriteEngine On
+ RewriteRule ^(/sub2/.*) /www/subdomain$1
+ # ...
+</VirtualHost>
+ </highlight>
<p>Due to the <directive module="core">ServerPath</directive>
directive a request to the URL
<code><VirtualHost></code> sections that are
substantially the same, for example:</p>
-<example>
-<pre>
+<highlight language="config">
<VirtualHost 111.22.33.44>
ServerName customer-1.example.com
DocumentRoot /www/hosts/customer-1.example.com/docs
DocumentRoot /www/hosts/customer-N.example.com/docs
ScriptAlias /cgi-bin/ /www/hosts/customer-N.example.com/cgi-bin
</VirtualHost>
-</pre>
-</example>
+</highlight>
<p>We wish to replace these multiple
<code><VirtualHost></code> blocks with a mechanism
href="#motivation">Motivation</a> section above
using <module>mod_vhost_alias</module>.</p>
-<example>
-# get the server name from the Host: header<br />
-UseCanonicalName Off<br />
-<br />
-# this log format can be split per-virtual-host based on the first field<br />
-# using the split-logfile utility.<br />
-LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
-CustomLog logs/access_log vcommon<br />
-<br />
-# include the server name in the filenames used to satisfy requests<br />
-VirtualDocumentRoot /www/hosts/%0/docs<br />
+<highlight language="config">
+# get the server name from the Host: header
+UseCanonicalName Off
+
+# this log format can be split per-virtual-host based on the first field
+# using the split-logfile utility.
+LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
+CustomLog logs/access_log vcommon
+
+# include the server name in the filenames used to satisfy requests
+VirtualDocumentRoot /www/hosts/%0/docs
VirtualScriptAlias /www/hosts/%0/cgi-bin
-</example>
+</highlight>
<p>This configuration can be changed into an IP-based virtual
hosting solution by just turning <code>UseCanonicalName
<code>/home/user/www</code>. It uses a single <code>cgi-bin</code>
directory instead of one per virtual host.</p>
-<example>
-UseCanonicalName Off<br />
-<br />
-LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
-CustomLog logs/access_log vcommon<br />
-<br />
-# include part of the server name in the filenames<br />
-VirtualDocumentRoot /home/%2/www<br />
-<br />
-# single cgi-bin directory<br />
-ScriptAlias /cgi-bin/ /www/std-cgi/<br />
-</example>
+<highlight language="config">
+UseCanonicalName Off
+
+LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
+CustomLog logs/access_log vcommon
+
+# include part of the server name in the filenames
+VirtualDocumentRoot /home/%2/www
+
+# single cgi-bin directory
+ScriptAlias /cgi-bin/ /www/std-cgi/
+</highlight>
<p>There are examples of more complicated
<code>VirtualDocumentRoot</code> settings in the
<code><VirtualHost></code> configuration sections, as shown
below.</p>
-<example>
-UseCanonicalName Off<br />
-<br />
-LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon<br />
-<br />
-<Directory /www/commercial><br />
-<indent>
- Options FollowSymLinks<br />
- AllowOverride All<br />
-</indent>
-</Directory><br />
-<br />
-<Directory /www/homepages><br />
-<indent>
- Options FollowSymLinks<br />
- AllowOverride None<br />
-</indent>
-</Directory><br />
-<br />
-<VirtualHost 111.22.33.44><br />
-<indent>
- ServerName www.commercial.example.com<br />
- <br />
- CustomLog logs/access_log.commercial vcommon<br />
- <br />
- VirtualDocumentRoot /www/commercial/%0/docs<br />
- VirtualScriptAlias /www/commercial/%0/cgi-bin<br />
-</indent>
-</VirtualHost><br />
-<br />
-<VirtualHost 111.22.33.45><br />
-<indent>
- ServerName www.homepages.example.com<br />
- <br />
- CustomLog logs/access_log.homepages vcommon<br />
- <br />
- VirtualDocumentRoot /www/homepages/%0/docs<br />
- ScriptAlias /cgi-bin/ /www/std-cgi/<br />
-</indent>
+<highlight language="config">
+UseCanonicalName Off
+
+LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
+
+<Directory /www/commercial>
+ Options FollowSymLinks
+ AllowOverride All
+</Directory>
+
+<Directory /www/homepages>
+ Options FollowSymLinks
+ AllowOverride None
+</Directory>
+
+<VirtualHost 111.22.33.44>
+ ServerName www.commercial.example.com
+
+ CustomLog logs/access_log.commercial vcommon
+
+ VirtualDocumentRoot /www/commercial/%0/docs
+ VirtualScriptAlias /www/commercial/%0/cgi-bin
</VirtualHost>
-</example>
+
+<VirtualHost 111.22.33.45>
+ ServerName www.homepages.example.com
+
+ CustomLog logs/access_log.homepages vcommon
+
+ VirtualDocumentRoot /www/homepages/%0/docs
+ ScriptAlias /cgi-bin/ /www/std-cgi/
+</VirtualHost>
+</highlight>
<note>
<title>Note</title>
negating the need for a DNS lookup. Logging will also have to be adjusted
to fit this system.</p>
-<example>
-# get the server name from the reverse DNS of the IP address<br />
-UseCanonicalName DNS<br />
-<br />
-# include the IP address in the logs so they may be split<br />
-LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon<br />
-CustomLog logs/access_log vcommon<br />
-<br />
-# include the IP address in the filenames<br />
-VirtualDocumentRootIP /www/hosts/%0/docs<br />
-VirtualScriptAliasIP /www/hosts/%0/cgi-bin<br />
-</example>
+<highlight language="config">
+# get the server name from the reverse DNS of the IP address
+UseCanonicalName DNS
+
+# include the IP address in the logs so they may be split
+LogFormat "%A %h %l %u %t \"%r\" %s %b" vcommon
+CustomLog logs/access_log vcommon
+
+# include the IP address in the filenames
+VirtualDocumentRootIP /www/hosts/%0/docs
+VirtualScriptAliasIP /www/hosts/%0/cgi-bin
+</highlight>
</section>