📄 examples.html.en
字号:
<h2><a name="ipport" id="ipport">Mixed port-based and ip-based virtual
hosts</a></h2>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
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 />
<span class="indent">
DocumentRoot /www/example1-80<br />
ServerName www.example1.com<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40:8080><br />
<span class="indent">
DocumentRoot /www/example1-8080<br />
ServerName www.example1.com<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.50:80><br />
<span class="indent">
DocumentRoot /www/example2-80<br />
ServerName www.example1.org<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.50:8080><br />
<span class="indent">
DocumentRoot /www/example2-8080<br />
ServerName www.example2.org<br />
</span>
</VirtualHost>
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="mixed" id="mixed">Mixed name-based and IP-based
vhosts</a></h2>
<p>On some of my addresses, I want to do name-based virtual hosts, and
on others, IP-based hosts.</p>
<div class="example"><h3>Server configuration</h3><p><code>
Listen 80<br />
<br />
NameVirtualHost 172.20.30.40<br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="indent">
DocumentRoot /www/example1<br />
ServerName www.example1.com<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="indent">
DocumentRoot /www/example2<br />
ServerName www.example2.org<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="indent">
DocumentRoot /www/example3<br />
ServerName www.example3.net<br />
</span>
</VirtualHost><br />
<br />
# IP-based<br />
<VirtualHost 172.20.30.50><br />
<span class="indent">
DocumentRoot /www/example4<br />
ServerName www.example4.edu<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.60><br />
<span class="indent">
DocumentRoot /www/example5<br />
ServerName www.example5.gov<br />
</span>
</VirtualHost>
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="proxy" id="proxy">Using <code>Virtual_host</code> and
mod_proxy together</a></h2>
<p>The following example allows a front-end machine to proxy a
virtual host through to a server running on another machine. In the
example, a virtual host of the same name is configured on a machine
at <code>192.168.111.2</code>. The <code class="directive"><a href="../mod/mod_proxy.html#proxypreservehost on">ProxyPreserveHost On</a></code> directive is
used so that the desired hostname is passed through, in case we are
proxying multiple hostnames to a single machine.</p>
<div class="example"><p><code>
<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>
</code></p></div>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="default" id="default">Using <code>_default_</code>
vhosts</a></h2>
<h3><a name="defaultallports" id="defaultallports"><code>_default_</code> vhosts
for all ports</a></h3>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
<VirtualHost _default_:*><br />
<span class="indent">
DocumentRoot /www/default<br />
</span>
</VirtualHost>
</code></p></div>
<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 <code class="directive"><a href="../mod/mod_alias.html#aliasmatch">AliasMatch</a></code> or
<code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> to rewrite any
request to a single information page (or script).</p>
<h3><a name="defaultdifferentports" id="defaultdifferentports"><code>_default_</code> vhosts
for different ports</a></h3>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
<VirtualHost _default_:80><br />
<span class="indent">
DocumentRoot /www/default80<br />
# ...<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost _default_:*><br />
<span class="indent">
DocumentRoot /www/default<br />
# ...<br />
</span>
</VirtualHost>
</code></p></div>
<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>
<h3><a name="defaultoneport" id="defaultoneport"><code>_default_</code> vhosts
for one port</a></h3>
<p>We want to have a default vhost for port 80, but no other default
vhosts.</p>
<div class="example"><h3>Server configuration</h3><p><code>
<VirtualHost _default_:80><br />
DocumentRoot /www/default<br />
...<br />
</VirtualHost>
</code></p></div>
<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>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="migrate" id="migrate">Migrating a name-based vhost to an
IP-based vhost</a></h2>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
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 />
<span class="indent">
DocumentRoot /www/example2<br />
ServerName www.example2.org<br />
# ...<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="indent">
DocumentRoot /www/example3<br />
ServerName www.example3.net<br />
ServerAlias *.example3.net<br />
# ...<br />
</span>
</VirtualHost>
</code></p></div>
<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>
</div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
<div class="section">
<h2><a name="serverpath" id="serverpath">Using the <code>ServerPath</code>
directive</a></h2>
<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>
<div class="example"><h3>Server configuration</h3><p><code>
NameVirtualHost 172.20.30.40<br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="indent">
# primary vhost<br />
DocumentRoot /www/subdomain<br />
RewriteEngine On<br />
RewriteRule ^/.* /www/subdomain/index.html<br />
# ...<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40><br />
DocumentRoot /www/subdomain/sub1<br />
<span class="indent">
ServerName www.sub1.domain.tld<br />
ServerPath /sub1/<br />
RewriteEngine On<br />
RewriteRule ^(/sub1/.*) /www/subdomain$1<br />
# ...<br />
</span>
</VirtualHost><br />
<br />
<VirtualHost 172.20.30.40><br />
<span class="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 />
</span>
</VirtualHost>
</code></p></div>
<p>Due to the <code class="directive"><a href="../mod/core.html#serverpath">ServerPath</a></code>
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 <code class="directive"><a href="../mod/mod_rewrite.html#rewriterule">RewriteRule</a></code> 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>
</div></div>
<div class="bottomlang">
<p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> |
<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p>
</div><div id="footer">
<p class="apache">Copyright 2006 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -