个人工具

Zope3宝典/设置Apache虚拟主机

来自Ubuntu中文

跳转至: 导航, 搜索

Chapter 4:Setting up a Virtual Hosting Environment with Apache (和Apache一起设置一个虚拟主机环境)


原文出处:The Zope 3 Developers Book - An Introduction for Python Programmers

原文作者:StephanRichter, zope.org

授权许可:创作共用协议

翻译人员:

  • 马 剑 <honeyday.mj@gmail.com>

校对人员:Leal, FireHare

适用版本:Zope 3

文章状态:校正阶段



Difficulty(难度)

Newcomer(新手)

Skills(技能)

        • You should have a basic understanding of the namespace syntax in Zope URLs.
          对Zope的URL里的名字空间语法有一个基本的理解。
        • You should be familiar with the Apache configuration file.
          你应该熟悉Apache的配置文件。

Problem/Task(问题/任务)

One of the most common tasks in the Zope world is to hide Zope behind the Apache web server in order to make use of all the nice features Apache provides, especially SSL encryption.
在Zope世界里一个最普遍的任务就是将Zope隐藏在Apache服务器的后面。这样做是为了使用所有Apache提供的良好的特性,最重要的是SSL加密特性。

Solution(解决方案)

Apache and other Web servers are commonly connected to Zope via rewrite rules specified in virtual hosts. Zope needs to interpret these requests correctly and provide meaningful output. You might think this is easy because you just have to point to the right URL of the Zope server. But this is only half the story. What about a URL that points to another object? To handle that situation, you need to tell Zope what the true virtual hosting address is. In Zope 3 this is accomplished using a special namespace called vh, which specifies the public address.
Apache及其他网络服务器是通过在虚拟主机中指定的重写规则来连接到Zope的。 正确的解释这些请求并提供有意义的输出是Zope的任务。你可能认为这是很容易的,因为你只须指向Zope服务器的正确的URL。 但是这只是问题的一半。指向另一个对象的URL是怎么样的呢?在这一点上你需要告诉Zope真实的虚拟主机住址是什么。 在Zope3中通过利用一个特殊的名字空间叫"vh"来完成的。它指定了"公共的"地址。

Before you can start setting up a virtual hosting environment on your server, you need to do the following:
在我们可以在我们的服务器上开始建立一个虚拟主机之前,我们将完成以下的清单:

Zope 3 uses its URL namespace capability to allow virtual hosting, so that no special component or coding practice is required, which means virtual hosting is always available. Generally, namespaces are specified using ++namespace++ as one element of the URL. For the vh namespace, for example, you have ++vh++Public-URL++. The ++ at the end of the URL is specific to the vh namespace. It signals the end of the public URL.
Zope3使用它的URL名字空间性能来提供虚拟主机,所以不需要特殊组件或者编码实践。这意味着虚拟主机是永远可用的。通常的,通过在URL中加入++namespace++元素来指定一个名字空间。对于vh名字空间,我们有++vh++Public-URL++。注意在 URL结尾处的++是特指给vh名字空间的。用标明了Public URL的末尾。

The namespace approach has the advantage that you can never lock yourself out due to misconfiguration. Some Zope 2 virtual hosting solutions have this problem and cause unnecessary headaches. In Zope 2 you also have to add an additional object. Zope 3 does not use any service or utility for this task, which makes virtual hosting support a very core functionality.
名字空间手段的好处是,一个人永远不会由于错误的配置而将她/他自己锁在外面。 有些Zope2的虚拟主机解决方案就有这个问题并且引来不必要的头痛。在Zope2中还不得不增加一个额外的对象。 Zope3对于这个任务不利用任何的服务或者实用工具,这些使虚拟主机成为一个非常核心的功能。

However, from an Apache 1.3 point of view, the setup of Zope 3 is very similar to that of Zope 2. In the httpd.conf file--usually found somewhere in /etc or /etc/httpd--you insert the following lines:
然而,从Apache的观点,建立的过程很类似于Zope2。在httpd.conf文件--通常的可以在/etc或者/etc/httpd下面找到--插入以下的行:

1   LoadModule proxy_module /path/to/apache/1.3/libproxy.so
2  
3   Listen 80
4  
5   NameVirtualHost *:80
6  
7   <VirtualHost *:80>
8     SSLDisable
9     ServerName www.example.com
10     RewriteEngine On
11     RewriteRule ^/site(/?.*) \
12   http://localhost:8080/site/++vh++http:www.example.com:80/site/++$1 \
13   [P,L]
14     CustomLog /var/log/apache/example.com/access.log combined
15     ErrorLog /var/log/apache/example.com/error.log
16   </VirtualHost>

In the preceding code block, note the following:
在前面的代码块中,注意下列几行:

  • Line 1: You load the module that allows rewriting and redirecting of the URL.
    行1:载入允给重写和URL的模块。
  • Line 3: You set up the Apache server for the default port 80.
    行3:设置Apache服务器的默认端口80。
  • Line 5: You declare all incoming requests on port 80 as virtual hosting sites.
    行5:声明所有在80端口上进入的请求作为虚拟主机的站点。
  • Lines 7-16: These are all specific configuration instructions for the virtual host at port 80.
    行7-16:这些全部是在80端口上虚拟主机的特别配置。
  • Line 8: This specifies to not use SSL encryption for communication. You are allowing only normal HTTP connections.
    行8:对于通讯不使用SSL加密。我们只允许正常的HTTP连接。

    Note that you need this line only if SSL is enabled (that is, if modssl is installed). If SSL is not installed, having this line will actually cause a configuration error during startup.
    注意:如果 SSL 能的话(也就是说如果 modssl 被安装)你只需要这一行。如果 SSL 没有安装,这一行在启动时将会引起一个配置错误。
  • Line 9: The virtual host is known as www.example.com to the outside world.
    行9:虚拟主机对外界的名字是www.example.com。
  • Line 10: You turn on the Rewrite Engine, basically telling Apache that this virtual host will rewrite and redirect requests.
    行10:开启重写引擎,基本上告诉Apache这个虚拟主机将要重写和重定向请求。
  • Line 11-13: The code in these lines should really appear on one line. It defines the actual rewrite rule, which says “If you find the URL after the hostname and port to begin with /site, then rewrite this URL to http://localhost:8080/site/++vh++http:www.example.com:80/site/++ plus whatever was behind /site.” For example, www.example.com:80/site/hello.html is rewritten to http://localhost:8080/site/++vh++http:www.example.com:80/site/++/hello.html.
    行11-13:在这些行里面的代码应该是在一行里的。它定义了实际的重写规则。这个规则说道:"如果你要找在主机名和端口后面以/site开头的 URL 的话,那么重定向这个URL到http://localhost:8080/site/+vh++http:www.example.com: 80/site/++后面加上任意的需要加的内容。" 例如:www.example.com:80/site/hello.html将被重写为http:// localhost:8080/site/++vh++http:www.example.com:80/site/++/hello.html。

    Note that the part after ++vh++ must strictly be of the form ¡protocol¿:¡host¿:¡port¿/¡path¿. Even if the port is 80 (the default), you have to specify it.
    注意:++vh++后面的部分必须严谨的遵守这样的样式:"协议:主机:端口/路径(protocol:host:port/path)"。即使端口是80,你也必须指定它。
  • Line 14: You define the location of the access log. This assumes that you have write access to the logfile directory, and you need to make sure the directory exists before you start Apache.
    行14:定义访问日志的位置。
  • Line 15: You define the location of the error log. 行15:定义错误日志的位置.
    Note: The preceding configuration steps have also been verified for Apache 2.0.
    注意:前面的配置步骤也可在 Apache2 下验证通过。

At this point you are done setting up Apache. It’s easy, isn’t it? All you need to do now is restart Apache so that the changes in configuration will take effect.
我们完成了。很简单,不是吗?所有你需要做的就是重启Apache,以便使改变的配置生效。

Nothing special needs to be configured on the Zope 3 side. Zope is actually totally unaware of the virtual hosting setup. Note that you do not have to map the URL www.example.com/site to localhost:8080/site; you can choose any location on the Zope server you like.
在Zope3方面不需要任何特殊的配置。 Zope实际上完全没有注意到虚拟主机的设置。 注意,你并不必需将www.example.com/site映射到localhost:8080/site,取而代之的是你可以选择Zope服务器上的任何位置。

You can now combine the preceding setup with all sorts of other Apache configurations as well (for example, SSL). You just use port 443 instead of 80 and enable SSL.
你现在可以联合其它的Apache配置项目到以上所述的设置中,例如SSL。只是使用443端口来代替80端口并启动SSL即可。

Note: One of the current problems in Zope 3 is that the XML navigation tree in the management interface does not work with virtual hosting because of the way it treats a URL.
注意:当前的问题:在管理界面里的XML导航树不支持虚拟主机,这是由于它看待URL的方式决定的。