diff options
Diffstat (limited to 'docs/manual/vhosts/examples.html.en')
-rw-r--r-- | docs/manual/vhosts/examples.html.en | 566 |
1 files changed, 566 insertions, 0 deletions
diff --git a/docs/manual/vhosts/examples.html.en b/docs/manual/vhosts/examples.html.en new file mode 100644 index 0000000..6c4f333 --- /dev/null +++ b/docs/manual/vhosts/examples.html.en @@ -0,0 +1,566 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head> +<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> +<!-- + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + This file is generated from xml source: DO NOT EDIT + XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + --> +<title>VirtualHost Examples - Apache HTTP Server Version 2.4</title> +<link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" /> +<link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" /> +<link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" /> +<script src="../style/scripts/prettify.min.js" type="text/javascript"> +</script> + +<link href="../images/favicon.ico" rel="shortcut icon" /></head> +<body id="manual-page"><div id="page-header"> +<p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/directives.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p> +<p class="apache">Apache HTTP Server Version 2.4</p> +<img alt="" src="../images/feather.png" /></div> +<div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div> +<div id="path"> +<a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.4</a> > <a href="./">Virtual Hosts</a></div><div id="page-content"><div id="preamble"><h1>VirtualHost Examples</h1> +<div class="toplang"> +<p><span>Available Languages: </span><a href="../en/vhosts/examples.html" title="English"> en </a> | +<a href="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> | +<a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> | +<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> | +<a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p> +</div> + + + <p>This document attempts to answer the commonly-asked questions about + setting up <a href="index.html">virtual hosts</a>. 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. + </p> + +</div> +<div id="quickview"><a href="https://www.apache.org/foundation/contributing.html" class="badge"><img src="https://www.apache.org/images/SupportApache-small.png" alt="Support Apache!" /></a><ul id="toc"><li><img alt="" src="../images/down.gif" /> <a href="#purename">Running several name-based web + sites on a single IP address.</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#twoips">Name-based hosts on more than one + IP address.</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#intraextra">Serving the same content on + different IP addresses (such as an internal and external + address).</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#port">Running different sites on different + ports.</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#ip">IP-based virtual hosting</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#ipport">Mixed port-based and ip-based virtual + hosts</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#mixed">Mixed name-based and IP-based + vhosts</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#proxy">Using <code>Virtual_host</code> and + mod_proxy together</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#default">Using <code>_default_</code> + vhosts</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#migrate">Migrating a name-based vhost to an + IP-based vhost</a></li> +<li><img alt="" src="../images/down.gif" /> <a href="#serverpath">Using the <code>ServerPath</code> + directive</a></li> +</ul><h3>See also</h3><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div> +<div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<h2><a name="purename" id="purename">Running several name-based web + sites on a single IP address.</a></h2> + + <p>Your server has multiple hostnames that resolve to a single address, + and you want to respond differently for <code>www.example.com</code> + and <code>www.example.org</code>.</p> + + <div class="note"><h3>Note</h3><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 + <code>hosts</code> entries.</p> + </div> + + <pre class="prettyprint lang-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></pre> + + + <p>The asterisks match all addresses, so the main server serves no + requests. Due to the fact that the virtual host with + <code>ServerName www.example.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 class="directive"><a href="../mod/core.html#servername">ServerName</a></code> directives, it will be served by this first + <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>.</p> + + <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> + + <div class="note"> + <h3>Note</h3> + + <p>You may replace <code>*</code> with a specific IP address + on the system. Such virtual hosts will only be used for + HTTP requests received on connection to the specified IP + address.</p> + + <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> + </div> + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<h2><a name="twoips" id="twoips">Name-based hosts on more than one + IP address.</a></h2> + + <div class="note"> + <h3>Note</h3> + <p>Any of the techniques discussed here can be extended to any + number of IP addresses.</p> + </div> + + <p>The server has two IP addresses. On one (<code>172.20.30.40</code>), we + 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> + + <pre class="prettyprint lang-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></pre> + + + <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.example.com</code>.</p> + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<h2><a name="intraextra" id="intraextra">Serving the same content on + different IP addresses (such as an internal and external + address).</a></h2> + + <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 class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code> section.</p> + + <pre class="prettyprint lang-config"><VirtualHost 192.168.1.1 172.20.30.40> + DocumentRoot "/www/server1" + ServerName server.example.com + ServerAlias server +</VirtualHost></pre> + + + <p>Now requests from both networks will be served from the same + <code class="directive"><a href="../mod/core.html#virtualhost"><VirtualHost></a></code>.</p> + + <div class="note"> + <h3>Note:</h3><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> + </div> + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<h2><a name="port" id="port">Running different sites on different + ports.</a></h2> + + <p>You have multiple domains going to the same IP and also want to + serve multiple ports. The example below illustrates that the name-matching + takes place after the best matching IP address and port combination + is determined.</p> + + <pre class="prettyprint lang-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></pre> + + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<h2><a name="ip" id="ip">IP-based virtual hosting</a></h2> + + <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.example.com</code> and <code>www.example.org</code> + respectively.</p> + + <pre class="prettyprint lang-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></pre> + + + <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> + + </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div> +<div class="section"> +<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.example.com</code> and <code>www.example.org</code> + respectively. In each case, we want to run hosts on ports 80 and + 8080.</p> + + <pre class="prettyprint lang-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></pre> + + + </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>Any address mentioned in the argument to a virtualhost that never + appears in another virtual host is a strictly IP-based virtual host.</p> + + <pre class="prettyprint lang-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></pre> + + + </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">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> + + <pre class="prettyprint lang-config"><VirtualHost *:*> + ProxyPreserveHost On + ProxyPass "/" "http://192.168.111.2/" + ProxyPassReverse "/" "http://192.168.111.2/" + ServerName hostname.example.com +</VirtualHost></pre> + + + </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> + + <pre class="prettyprint lang-config"><VirtualHost _default_:*> + DocumentRoot "/www/default" +</VirtualHost></pre> + + + <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> + + <pre class="prettyprint lang-config"><VirtualHost _default_:80> + DocumentRoot "/www/default80" + # ... +</VirtualHost> + +<VirtualHost _default_:*> + DocumentRoot "/www/default" + # ... +</VirtualHost></pre> + + + <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> + + <pre class="prettyprint lang-config"><VirtualHost _default_:80> + DocumentRoot "/www/default" +... +</VirtualHost></pre> + + + <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> + + <p>Any use of <code>*</code> in a virtual host declaration will have + higher precedence than <code>_default_</code>.</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.example.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.</p> + + <p> + 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> + + <pre class="prettyprint lang-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></pre> + + + <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> + + <pre class="prettyprint lang-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></pre> + + + <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.</p> + + <p>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.</p> + + <p>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="../fr/vhosts/examples.html" hreflang="fr" rel="alternate" title="Français"> fr </a> | +<a href="../ja/vhosts/examples.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a> | +<a href="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a> | +<a href="../tr/vhosts/examples.html" hreflang="tr" rel="alternate" title="Türkçe"> tr </a></p> +</div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Libera.chat, or sent to our <a href="https://httpd.apache.org/lists.html">mailing lists</a>.</div> +<script type="text/javascript"><!--//--><![CDATA[//><!-- +var comments_shortname = 'httpd'; +var comments_identifier = 'http://httpd.apache.org/docs/2.4/vhosts/examples.html'; +(function(w, d) { + if (w.location.hostname.toLowerCase() == "httpd.apache.org") { + d.write('<div id="comments_thread"><\/div>'); + var s = d.createElement('script'); + s.type = 'text/javascript'; + s.async = true; + s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier; + (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s); + } + else { + d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>'); + } +})(window, document); +//--><!]]></script></div><div id="footer"> +<p class="apache">Copyright 2023 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="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!-- +if (typeof(prettyPrint) !== 'undefined') { + prettyPrint(); +} +//--><!]]></script> +</body></html>
\ No newline at end of file |