📄 examples.html.en
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!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><!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This file is generated from xml source: DO NOT EDIT XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX --><title>VirtualHost Examples - Apache HTTP Server</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 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="../faq/">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p><p class="apache">Apache HTTP Server Version 2.0</p><img alt="" src="../images/feather.gif" /></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.0</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="../ko/vhosts/examples.html" hreflang="ko" rel="alternate" title="Korean"> ko </a></p></div> <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. </p></div><div id="quickview"><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></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 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> <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 hosts entries.</p> </div> <div class="example"><h3>Server configuration</h3><p><code> # Ensure that Apache listens on port 80<br /> Listen 80<br /> <br /> # Listen for virtual host requests on all IP addresses<br /> NameVirtualHost *:80<br /> <br /> <VirtualHost *:80><br /> <span class="indent"> DocumentRoot /www/example1<br /> ServerName www.example1.com<br /> <br /> # Other directives here<br /> <br /> </span> </VirtualHost><br /> <br /> <VirtualHost *:80><br /> <span class="indent"> DocumentRoot /www/example2<br /> ServerName www.example2.org<br /> <br /> # Other directives here<br /> <br /> </span> </VirtualHost> </code></p></div> <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> <div class="note"> <h3>Note</h3> <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> <div class="example"><p><code> NameVirtualHost 172.20.30.40<br /> <br /> <VirtualHost 172.20.30.40><br /> # etc ... </code></p></div> <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> <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><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.domain.com</code> and on the other (<code>172.20.30.50</code>), we will serve two or more virtual hosts.</p> <div class="example"><h3>Server configuration</h3><p><code> 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 /> <span class="indent"> DocumentRoot /www/example1<br /> ServerName www.example1.com<br /> <br /> # Other directives here ...<br /> <br /> </span> </VirtualHost><br /> <br /> <VirtualHost 172.20.30.50><br /> <span class="indent"> DocumentRoot /www/example2<br /> ServerName www.example2.org<br /> <br /> # Other directives here ...<br /> <br /> </span> </VirtualHost> </code></p></div> <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> </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>VirtualHost</code> section.</p> <div class="example"><h3>Server configuration</h3><p><code> NameVirtualHost 192.168.1.1<br /> NameVirtualHost 172.20.30.40<br /> <br /> <VirtualHost 192.168.1.1 172.20.30.40><br /> <span class="indent"> DocumentRoot /www/server1<br /> ServerName server.example.com<br /> ServerAlias server<br /> </span> </VirtualHost> </code></p></div> <p>Now requests from both networks will be served from the same <code>VirtualHost</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. 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> <div class="example"><h3>Server configuration</h3><p><code> 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 /> <span class="indent"> ServerName www.example1.com<br /> DocumentRoot /www/domain-80<br /> </span> </VirtualHost><br /> <br /> <VirtualHost 172.20.30.40:8080><br /> <span class="indent"> ServerName www.example1.com<br /> DocumentRoot /www/domain-8080<br /> </span> </VirtualHost><br /> <br /> <VirtualHost 172.20.30.40:80><br /> <span class="indent"> ServerName www.example2.org<br /> DocumentRoot /www/otherdomain-80<br /> </span> </VirtualHost><br /> <br /> <VirtualHost 172.20.30.40:8080><br /> <span class="indent"> ServerName www.example2.org<br /> DocumentRoot /www/otherdomain-8080<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="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.example1.com</code> and <code>www.example2.org</code> respectively.</p> <div class="example"><h3>Server configuration</h3><p><code> Listen 80<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.50><br /> <span class="indent"> DocumentRoot /www/example2<br /> ServerName www.example2.org<br /> </span> </VirtualHost> </code></p></div> <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">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -