| <?xml version='1.0' encoding='UTF-8' ?> |
| <!DOCTYPE manualpage SYSTEM "../style/manualpage.dtd"> |
| <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?> |
| <!-- $Revision: 1.8 $ --> |
| |
| <!-- |
| Copyright 2002-2004 The Apache Software Foundation |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <manualpage metafile="examples.xml.meta"> |
| <parentdocument href="./">Virtual Hosts</parentdocument> |
| <title>VirtualHost Examples</title> |
| |
| <summary> |
| |
| <p>This document attempts to answer the commonly-asked questions about |
| setting up virtual hosts. These scenarios are those involving multiple |
| web sites running on a single server, via <a |
| href="name-based.html">name-based</a> or <a |
| href="ip-based.html">IP-based</a> virtual hosts. A document should be |
| coming soon about running sites on several servers behind a single |
| proxy server.</p> |
| |
| </summary> |
| |
| <section id="purename"><title>Running several name-based web |
| sites on a single IP address.</title> |
| |
| <p>Your server has a single IP address, and multiple aliases (CNAMES) |
| point to this machine in DNS. You want to run a web server for |
| <code>www.example1.com</code> and <code>www.example2.org</code> on this |
| machine.</p> |
| |
| <note><title>Note</title><p>Creating virtual |
| host configurations on your Apache server does not magically |
| cause DNS entries to be created for those host names. You |
| <em>must</em> have the names in DNS, resolving to your IP |
| address, or nobody else will be able to see your web site. You |
| can put entries in your <code>hosts</code> file for local |
| testing, but that will work only from the machine with those |
| hosts entries.</p> |
| </note> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| # Ensure that Apache listens on port 80<br /> |
| Listen 80<br /> |
| <br /> |
| # Listen for virtual host requests on all IP addresses<br /> |
| NameVirtualHost *<br /> |
| <br /> |
| <VirtualHost *><br /> |
| <indent> |
| DocumentRoot /www/example1<br /> |
| ServerName www.example1.com<br /> |
| <br /> |
| # Other directives here<br /> |
| <br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost *><br /> |
| <indent> |
| DocumentRoot /www/example2<br /> |
| ServerName www.example2.org<br /> |
| <br /> |
| # Other directives here<br /> |
| <br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| <p>The asterisks match all addresses, so the main server serves no |
| requests. Due to the fact that <code>www.example1.com</code> is first |
| in the configuration file, it has the highest priority and can be seen |
| as the <cite>default</cite> or <cite>primary</cite> server. That means |
| that if a request is received that does not match one of the specified |
| <code>ServerName</code> directives, it will be served by this first |
| <code>VirtualHost</code>.</p> |
| |
| <note> |
| <title>Note</title> |
| |
| <p>You can, if you wish, replace <code>*</code> with the actual |
| IP address of the system. In that case, the argument to |
| <code>VirtualHost</code> <em>must</em> match the argument to |
| <code>NameVirtualHost</code>:</p> |
| |
| <example> |
| NameVirtualHost 172.20.30.40<br /> |
| <br /> |
| <VirtualHost 172.20.30.40><br /> |
| # etc ... |
| </example> |
| |
| <p>However, it is additionally useful to use <code>*</code> |
| on systems where the IP address is not predictable - for |
| example if you have a dynamic IP address with your ISP, and |
| you are using some variety of dynamic DNS solution. Since |
| <code>*</code> matches any IP address, this configuration |
| would work without changes whenever your IP address |
| changes.</p> |
| </note> |
| |
| <p>The above configuration is what you will want to use in almost |
| all name-based virtual hosting situations. The only thing that this |
| configuration will not work for, in fact, is when you are serving |
| different content based on differing IP addresses or ports.</p> |
| |
| </section> |
| |
| <section id="twoips"><title>Name-based hosts on more than one |
| IP address.</title> |
| |
| <note> |
| <title>Note</title><p>Any of the |
| techniques discussed here can be extended to any number of IP |
| addresses.</p> |
| </note> |
| |
| <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we |
| will serve the "main" server, <code>server.domain.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.domain.com<br /> |
| DocumentRoot /www/mainserver<br /> |
| <br /> |
| # This is the other address<br /> |
| NameVirtualHost 172.20.30.50<br /> |
| <br /> |
| <VirtualHost 172.20.30.50><br /> |
| <indent> |
| DocumentRoot /www/example1<br /> |
| ServerName www.example1.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.example2.org<br /> |
| <br /> |
| # Other directives here ...<br /> |
| <br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| <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 |
| unknown hostname, or no <code>Host:</code> header, will be served from |
| <code>www.example1.com</code>.</p> |
| |
| </section> |
| |
| <section id="intraextra"><title>Serving the same content on |
| different IP addresses (such as an internal and external |
| address).</title> |
| |
| <p>The server machine has two IP addresses (<code>192.168.1.1</code> |
| and <code>172.20.30.40</code>). The machine is sitting between an |
| internal (intranet) network and an external (internet) network. Outside |
| of the network, the name <code>server.example.com</code> resolves to |
| the external address (<code>172.20.30.40</code>), but inside the |
| network, that same name resolves to the internal address |
| (<code>192.168.1.1</code>).</p> |
| |
| <p>The server can be made to respond to internal and external requests |
| with the same content, with just one <code>VirtualHost</code> |
| section.</p> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| NameVirtualHost 192.168.1.1<br /> |
| NameVirtualHost 172.20.30.40<br /> |
| <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> |
| |
| <p>Now requests from both networks will be served from the same |
| <code>VirtualHost</code>.</p> |
| |
| <note> |
| <title>Note:</title><p>On the internal |
| network, one can just use the name <code>server</code> rather |
| than the fully qualified host name |
| <code>server.example.com</code>.</p> |
| |
| <p>Note also that, in the above example, you can replace the list |
| of IP addresses with <code>*</code>, which will cause the server to |
| respond the same on all addresses.</p> |
| </note> |
| |
| </section> |
| |
| <section id="port"><title>Running different sites on different |
| ports.</title> |
| |
| <p>You have multiple domains going to the same IP and also want to |
| serve multiple ports. By defining the ports in the "NameVirtualHost" |
| tag, you can allow this to work. If you try using <VirtualHost |
| name:port> without the NameVirtualHost name:port or you try to use |
| the Listen directive, your configuration will not work.</p> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| Listen 80<br /> |
| Listen 8080<br /> |
| <br /> |
| NameVirtualHost 172.20.30.40:80<br /> |
| NameVirtualHost 172.20.30.40:8080<br /> |
| <br /> |
| <VirtualHost 172.20.30.40:80><br /> |
| <indent> |
| ServerName www.example1.com<br /> |
| DocumentRoot /www/domain-80<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40:8080><br /> |
| <indent> |
| ServerName www.example1.com<br /> |
| DocumentRoot /www/domain-8080<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40:80><br /> |
| <indent> |
| ServerName www.example2.org<br /> |
| DocumentRoot /www/otherdomain-80<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40:8080><br /> |
| <indent> |
| ServerName www.example2.org<br /> |
| DocumentRoot /www/otherdomain-8080<br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| </section> |
| |
| <section id="ip"><title>IP-based virtual hosting</title> |
| |
| <p>The server has two IP addresses (<code>172.20.30.40</code> and |
| <code>172.20.30.50</code>) which resolve to the names |
| <code>www.example1.com</code> and <code>www.example2.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.example1.com<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.50><br /> |
| <indent> |
| DocumentRoot /www/example2<br /> |
| ServerName www.example2.org<br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| <p>Requests for any address not specified in one of the |
| <code><VirtualHost></code> directives (such as |
| <code>localhost</code>, for example) will go to the main server, if |
| there is one.</p> |
| |
| </section> |
| |
| <section id="ipport"><title>Mixed port-based and ip-based virtual |
| hosts</title> |
| |
| <p>The server machine has two IP addresses (<code>172.20.30.40</code> and |
| <code>172.20.30.50</code>) which resolve to the names |
| <code>www.example1.com</code> and <code>www.example2.org</code> |
| 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.example1.com<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40:8080><br /> |
| <indent> |
| DocumentRoot /www/example1-8080<br /> |
| ServerName www.example1.com<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.50:80><br /> |
| <indent> |
| DocumentRoot /www/example2-80<br /> |
| ServerName www.example1.org<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.50:8080><br /> |
| <indent> |
| DocumentRoot /www/example2-8080<br /> |
| ServerName www.example2.org<br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| </section> |
| |
| <section id="mixed"><title>Mixed name-based and IP-based |
| vhosts</title> |
| |
| <p>On some of my addresses, I want to do name-based virtual hosts, and |
| on others, IP-based hosts.</p> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| Listen 80<br /> |
| <br /> |
| NameVirtualHost 172.20.30.40<br /> |
| <br /> |
| <VirtualHost 172.20.30.40><br /> |
| <indent> |
| DocumentRoot /www/example1<br /> |
| ServerName www.example1.com<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40><br /> |
| <indent> |
| DocumentRoot /www/example2<br /> |
| ServerName www.example2.org<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40><br /> |
| <indent> |
| DocumentRoot /www/example3<br /> |
| ServerName www.example3.net<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| # IP-based<br /> |
| <VirtualHost 172.20.30.50><br /> |
| <indent> |
| DocumentRoot /www/example4<br /> |
| ServerName www.example4.edu<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.60><br /> |
| <indent> |
| DocumentRoot /www/example5<br /> |
| ServerName www.example5.gov<br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| </section> |
| |
| <section id="default"><title>Using <code>_default_</code> |
| vhosts</title> |
| |
| <section id="defaultallports"><title><code>_default_</code> vhosts |
| for all ports</title> |
| |
| <p>Catching <em>every</em> request to any unspecified IP address and |
| 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> |
| |
| <p>Using such a default vhost with a wildcard port effectively prevents |
| any request going to the main server.</p> |
| |
| <p>A default vhost never serves a request that was sent to an |
| address/port that is used for name-based vhosts. If the request |
| contained an unknown or no <code>Host:</code> header it is always |
| served from the primary name-based vhost (the vhost for that |
| address/port appearing first in the configuration file).</p> |
| |
| <p>You can use <directive module="mod_alias">AliasMatch</directive> or |
| <directive module="mod_rewrite">RewriteRule</directive> to rewrite any |
| request to a single information page (or script).</p> |
| </section> |
| |
| <section id="defaultdifferentports"><title><code>_default_</code> vhosts |
| for different ports</title> |
| |
| <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> |
| |
| <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 |
| to an unspecified IP address. The main server is never used to serve a |
| request.</p> |
| </section> |
| |
| <section id="defaultoneport"><title><code>_default_</code> vhosts |
| for one port</title> |
| |
| <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> |
| |
| <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 |
| served from the main server.</p> |
| </section> |
| |
| </section> |
| |
| <section id="migrate"><title>Migrating a name-based vhost to an |
| IP-based vhost</title> |
| |
| <p>The name-based vhost with the hostname |
| <code>www.example2.org</code> (from our <a |
| href="#name">name-based</a> example, setup 2) should get its own IP |
| address. To avoid problems with name servers or proxies who cached the |
| old IP address for the name-based vhost we want to provide both |
| variants during a migration phase.<br /> |
| The solution is easy, because we can simply add the new IP address |
| (<code>172.20.30.50</code>) to the <code>VirtualHost</code> |
| directive.</p> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| Listen 80<br /> |
| ServerName www.example1.com<br /> |
| DocumentRoot /www/example1<br /> |
| <br /> |
| NameVirtualHost 172.20.30.40<br /> |
| <br /> |
| <VirtualHost 172.20.30.40 172.20.30.50><br /> |
| <indent> |
| DocumentRoot /www/example2<br /> |
| ServerName www.example2.org<br /> |
| # ...<br /> |
| </indent> |
| </VirtualHost><br /> |
| <br /> |
| <VirtualHost 172.20.30.40><br /> |
| <indent> |
| DocumentRoot /www/example3<br /> |
| ServerName www.example3.net<br /> |
| ServerAlias *.example3.net<br /> |
| # ...<br /> |
| </indent> |
| </VirtualHost> |
| </example> |
| |
| <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 |
| vhost).</p> |
| |
| </section> |
| |
| <section id="serverpath"><title>Using the <code>ServerPath</code> |
| directive</title> |
| |
| <p>We have a server with two name-based vhosts. In order to match the |
| correct virtual host a client must send the correct <code>Host:</code> |
| header. Old HTTP/1.0 clients do not send such a header and Apache has |
| no clue what vhost the client tried to reach (and serves the request |
| from the primary vhost). To provide as much backward compatibility as |
| possible we create a primary vhost which returns a single page |
| containing links with an URL prefix to the name-based virtual |
| hosts.</p> |
| |
| <example> |
| <title>Server configuration</title> |
| |
| NameVirtualHost 172.20.30.40<br /> |
| <br /> |
| <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> |
| |
| <p>Due to the <directive module="core">ServerPath</directive> |
| directive a request to the URL |
| <code>http://www.sub1.domain.tld/sub1/</code> is <em>always</em> served |
| from the sub1-vhost.<br /> A request to the URL |
| <code>http://www.sub1.domain.tld/</code> is only |
| served from the sub1-vhost if the client sent a correct |
| <code>Host:</code> header. If no <code>Host:</code> header is sent the |
| client gets the information page from the primary host.<br /> |
| Please note that there is one oddity: A request to |
| <code>http://www.sub2.domain.tld/sub1/</code> is also served from the |
| sub1-vhost if the client sent no <code>Host:</code> header.<br /> |
| The <directive module="mod_rewrite">RewriteRule</directive> directives |
| are used to make sure that a client which sent a correct |
| <code>Host:</code> header can use both URL variants, <em>i.e.</em>, |
| with or without URL prefix.</p> |
| |
| </section> |
| |
| </manualpage> |