【翻译】VirtualHost举例讲解(初稿)

翻译一篇Apache官网的,关于VirtualHost的举例介绍,原文点我,译文如下:

VirtualHost Examples

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 name-based or IP-based virtual hosts.
该文件旨在解答大家普遍关于virtual hosts的设置问题;这个系列包括:在一个服务器上跑多个web 网站,通过基于名称或者基于IP 的虚拟主机。

Running several name-based web sites on a single IP address.

在一个IP地址上跑多个基于name的web 网站;

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 www.example1.com and www.example2.org on this machine.
你的服务器有一个IP地址,并且多个别名站点(别名 事器将每个主机名映照)对应这个DNS(Domain Name System,域名系统)上的机器。你想跑一个web服务,例如 www.example1.com 和 www.example2.org在这个机器上;

Note Creating virtual host configurations on your Apache server does not magically cause DNS entries to be created for those host names.
You must 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 hosts file for local testing, but that will work only from the
machine with those hosts entries.*

注意创建一个虚拟主机配置在你的Apache 服务器上不会无缘无故地为了这个主机名创建DNS入口;你必须在DNS中有这个名字,可以解析了你的IP地址,或者没其他人能够看到你的网站;你可以为了本地测试,在你的hosts文件中放一个入口,但是只有这个机器有这些hosts入口,他才会工作;

Server configuration //服务配置

# Ensure that Apache listens on port 80 //确保Apache监控80
Listen 80

# Listen for virtual host requests on all IP addresses 监听
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here

</VirtualHost>

The asterisks match all addresses, so the main server serves no requests. Due to the fact that www.example1.com is first in the configuration file, it has the highest priority and can be seen as the default or primary server. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first VirtualHost.

这里的星号匹配所有的地址,因此主服务器没有请求;由于www.example1.com是第一个配置文件的缘故,他享有最高的优先权,并且可以被视为默认或者最高级的服务器;这意味着,如果收到一个请求,这个请求不匹配任何一个特殊的服务器名指令,他会被第一个虚拟主机服

Note

You can, if you wish, replace * with the actual IP address of the system. In that case, the argument to VirtualHost must match the argument to NameVirtualHost: NameVirtualHost 172.20.30.40

Name-based hosts on more than one IP address.

在多个IP地址的基于名称的主机

Note

Any of the techniques discussed here can be extended to any number of
IP addresses.
注意
这里讨论的任何一个技巧可以被扩展到任何数量的IP地址;

The server has two IP addresses. On one (172.20.30.40), we will serve the “main” server, server.domain.com and on the other (172.20.30.50), we will serve two or more virtual hosts.
服务器有两个IP地址,一个是172.20.30.40,我们设定为主服务器server.domain.com并且另一个172.20.30.50,我们将服务两个或多个虚拟主机;

Server configuration

Listen 80

# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver

# This is the other address
NameVirtualHost 172.20.30.50

<VirtualHost 172.20.30.50>
DocumentRoot /www/example1
ServerName www.example1.com

# Other directives here ...

</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org

# Other directives here ...

</VirtualHost>

Any request to an address other than 172.20.30.50 will be served from the main server. A request to 172.20.30.50 with an unknown hostname, or no Host: header, will be served from www.example1.com.

任何一个不是172.20.30.50的地址将被主服务器服务(ps:就是172.20.30.40);一个带有未知的主机名或者没有主机的头部(header)向172.20.30.50的请求,将被www.example1.com服务;

Serving the same content on different IP addresses (such as an internal and external address).

在不同的IP地址上服务相同的内容(例如一个内部和外部的地址)

The server machine has two IP addresses (192.168.1.1 and 172.20.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name server.example.com resolves to the external address (172.20.30.40), but inside the network, that same name resolves to the internal address (192.168.1.1).
服务器的机器有两个地址:(192.168.1.1 和172.20.30.40);服务器的机器被设定成一个内网,一个外网;外网的是:名字 server.example.com解析为(172.20.30.40),但是内网是相同的名字解析为内部的地址(192.168.1.1)

The server can be made to respond to internal and external requests with the same content, with just one VirtualHost section.
服务器可以被设计成使用相同的内容(content)响应内部和外部的请求,仅仅使用一个虚拟主机的部分;

Server configuration

NameVirtualHost 192.168.1.1
NameVirtualHost 172.20.30.40

<VirtualHost 192.168.1.1 172.20.30.40>
DocumentRoot /www/server1
ServerName server.example.com
ServerAlias server
</VirtualHost>

Now requests from both networks will be served from the same VirtualHost.
现在,无论是内网还是外网都将被同一个虚拟主机服务;

Note:

On the internal network, one can just use the name server rather than
the fully qualified host name server.example.com. Note also that, in
the above example, you can replace the list of IP addresses with *,
which will cause the server to respond the same on all addresses.
注意
在内部网络,一个服务器只可以使用服务器的名字,而不能使用全称描述的主机服务器的名字例如server.example.com。还应注意在上面的例子中,你可以使用星号替换一系列的IP地址,这将导致服务器响应所有的地址在相同的服务器;

Running different sites on different ports.

在不同的端口号下跑不同的站点

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.
你有多个域名,前往相同的IP并且任然希望服务多个端口;当在“NameVirtualHost”标签里定义端口号是,你可以允许这个来工作。如果你想使用<VirtualHost name:port> 没有NameVirtualHost name:port或者你想使用监听的指定,你的配置将不被工作;

Server configuration

Listen 80
Listen 8080

NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080

<VirtualHost 172.20.30.40:80>
ServerName www.example1.com
DocumentRoot /www/domain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example1.com
DocumentRoot /www/domain-8080
</VirtualHost>

<VirtualHost 172.20.30.40:80>
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
</VirtualHost>

IP-based virtual hosting

基于IP的虚拟主机

The server has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively.
服务器有两个IP地址 (172.20.30.40 和172.20.30.50),这将被分别独立地解析成www.example1.com 和www.example2.org;

Server configuration

Listen 80

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

Requests for any address not specified in one of the <VirtualHost> directives (such as localhost, for example) will go to the main server, if there is one.
请求任何地址,不是特定的某一个虚拟主机指令(例如localhost),如果那里只有一个服务器主机,他将前往主服务器;

Mixed port-based and ip-based virtual hosts

混合的基于端口号和基于IP的虚拟主机地址;

The server machine has two IP addresses (172.20.30.40 and 172.20.30.50) which resolve to the names www.example1.com and www.example2.org respectively. In each case, we want to run hosts on ports 80 and 8080.
服务器的机器有两个地址(172.20.30.40 和 172.20.30.50),这将被独立解析成名为:www.example1.com 和 www.example2.org ;在各自情况下,我们想让hosts在80端口和8080端口跑;

Server configuration

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.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40:8080>
DocumentRoot /www/example1-8080
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.50:80>
DocumentRoot /www/example2-80
ServerName www.example1.org
</VirtualHost>

<VirtualHost 172.20.30.50:8080>
DocumentRoot /www/example2-8080
ServerName www.example2.org
</VirtualHost>

Mixed name-based and IP-based vhosts

混合的基于名字和基于IP地址的vhosts

On some of my addresses, I want to do name-based virtual hosts, and on others, IP-based hosts.
我的某些地址,我希望基于名字或者基于IP地址的虚拟主机映射;

Server configuration

Listen 80

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40>
DocumentRoot /www/example1
ServerName www.example1.com
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example2
ServerName www.example2.org
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
</VirtualHost>

# IP-based
<VirtualHost 172.20.30.50>
DocumentRoot /www/example4
ServerName www.example4.edu
</VirtualHost>

<VirtualHost 172.20.30.60>
DocumentRoot /www/example5
ServerName www.example5.gov
</VirtualHost>

Using Virtual_host and mod_proxy together

同时使用Virtual_host和mod_proxy;

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 192.168.111.2. The ProxyPreserveHost On directive is used so that the desired hostname is passed through, in case we are proxying multiple hostnames to a single machine.
下面的例子允许一个前后台机器通过一个服务器跑在另一个机器上来代理一个虚拟主机地址;例如,一个虚拟主机地址被配置在一个机器上:192.168.111.2;如果我们代理了多个服务器名在一个机器上;
代,理的预服务器主机地址独立的被使用;因此需要的hostname被忽略了。

<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://192.168.111.2/
ProxyPassReverse / http://192.168.111.2/
ServerName hostname.example.com
</VirtualHost>

Using default vhosts

使用默认的vhosts

default vhosts for all ports
Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
默认的vhosts对所有的端口号;

Server configuration

<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>

Using such a default vhost with a wildcard port effectively prevents any request going to the main server.

使用这样一个带有通配端口号的默认的vhost能有效的预防任何请求去访问主服务器;

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 Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).
在被发送了一个地址或端口号使用了基于名称的vhosts,一个默认的vhost不会服务一个请求;如果请求包含一个未知或者没有Host的头部信息;他将一直被基于名字的vhost服务;(对应地址和端口号的vhost在配置文件的第一)

You can use AliasMatch or RewriteRule to rewrite any request to a single information page (or script).
你可以使用别名匹配或者重写规则来重写任何对于单个信息的页面(或script)请求

default vhosts for different ports
Same as setup 1, but the server listens on several ports and we want to use a second default vhost for port 80.
不同的端口的_default_vhosts;
和第一步设置一样,但是服务器再服务端口上监听并且我们希望为80端口使用第二个_default_vhost

Server configuration

<VirtualHost _default_:80>
DocumentRoot /www/default80
# ...
</VirtualHost>

<VirtualHost _default_:*>
DocumentRoot /www/default
# ...
</VirtualHost>

The default vhost for port 80 (which must 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.
80端口默认的vhost(在任何带通配符端口的默认的vhost这个端口必须显示);抓取所有的请求将被发送给一个非特定的IP地址;主服务器永远不会服务一个请求;

default vhosts for one port
We want to have a default vhost for port 80, but no other default vhosts.
_default_vhosts 对应一个端口
我们希望为80端口配一个默认的vhost,而不是默认的vhosts。

Server configuration

<VirtualHost _default_:80>
DocumentRoot /www/default
...
</VirtualHost>

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.
一个在80端口号上向未明确定义的请求会被默认的vhost服务;任何向一个非特定的地址和端口其他的请求会被主服务器服务;

Migrating a name-based vhost to an IP-based vhost

将基于名称的vhost转移为基于基于IP的vhost;

The name-based vhost with the hostname www.example2.org (from our name-based 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.
The solution is easy, because we can simply add the new IP address (172.20.30.50) to the VirtualHost directive.
基于名字的vhost带有hostname:www.example2.org (从我们的基于名字的example,setup 2)
必须获取他们的自己的IP地址;为了避免名称服务器或者代理为了基于名称的vhost(抓取旧的IP地址);我们希望通过转移语句提供两种时期;解决的办法很容易,因为我们可以简单的在VirtualHost命令中添加新的IP地址(172.20.30.50);

Server configuration

Listen 80
ServerName www.example1.com
DocumentRoot /www/example1

NameVirtualHost 172.20.30.40

<VirtualHost 172.20.30.40 172.20.30.50>
DocumentRoot /www/example2
ServerName www.example2.org
# ...
</VirtualHost>

<VirtualHost 172.20.30.40>
DocumentRoot /www/example3
ServerName www.example3.net
ServerAlias *.example3.net
# ...
</VirtualHost>

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).
这个vhost现在可以通过新的地址(作为一个基于IP的vhost)和通过旧的地址(作为一个基于名字的vhost)

Using the ServerPath directive

使用ServerPath指令

We have a server with two name-based vhosts. In order to match the correct virtual host a client must send the correct Host: 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.
我们有两个基于名字的vhosts服务器;为了匹配正确的虚拟主机,一个客户端必须发送正确的主机头部信息;旧的HTTP/1.0 客户端不会发送一个头部信息并且Apache没有vhost的线索;客户端希望访问(从基础的vhost服务请求)。为了尽可能提供例如反向的兼容性,我们创造一个基本的vhost,这个vhost会返回一个单独的包含基于名字的虚拟主机的URL连接前缀的页面;

Server configuration

NameVirtualHost 172.20.30.40

<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>

Due to the ServerPath directive a request to the URL http://www.sub1.domain.tld/sub1/ is always served from the sub1-vhost.
因为服务器路径指定,一个向URL http://www.sub1.domain.tld/sub1/的请求总是被sub1-vhost服务;

A request to the URL http://www.sub1.domain.tld/ is only served from the sub1-vhost if the client sent a correct Host: header. If no Host: header is sent the client gets the information page from the primary host.
如果发送了正确地Host的头部信息,一个从 sub1-vhost客户端向URLhttp://www.sub1.domain.tld/ 的请求是唯一被服务的;

Please note that there is one oddity: A request to http://www.sub2.domain.tld/sub1/ is also served from the sub1-vhost if the client sent no Host: header.
请注意这里有一个怪事:如果客户端没有发送Host的头部信息,一个向 http://www.sub2.domain.tld/sub1/ 的请求也会被从sub1-vhost被服务;

The RewriteRule directives are used to make sure that a client which sent a correct Host: header can use both URL variants, i.e., with or without URL prefix.
重写信息指令被用于确保一个发送正确的Host头部信息的客户端可以随便使用URL变量,无论带不带URL前缀;

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值