theMorgue.org
Pushing the Limits of Home Electronics  

Serving Zope Virtual Hosts using Apache

1. Overview

A common thing for people to do with their websites is to set up virtual hosts to point to sub-domains, but still run just one web server. This allows you to do things like send accesses to support.yourdomain.com to your internal server 192.168.1.1/support. This has practical purposes like load balancing, but I just use it redirect sub-domains to deeply nested folders in my Zope server.

Setting up a virtual host is not all that difficult, but requires several steps. These steps are described in the following sections.

2. Server Setup

2.1. Configuring Sub-Domains in the DNS

The first thing you need to do is tell the world what hostnames and sub-domains are available on your site. If you use http://www.mydomain.com or http://www.dyndns.org, you can simply set up a CNAME that points your new sub-domain or hostname to your registered domain. For example, if you owned the domain yourdomain.com and wanted to create a virtual host call support.yourdomain.com, you would create a CNAME from support.yourdomain.com to yourdomain.com.

Once you do this, you may have to wait for a few hours for the DNS records to be updated before these names will work from anywhere on the Internet.

2.2. Configuring the Virtual Hosts in Apache

This section assumes that you have a working Apache installation and that it is running at the IP address 120.1.1.1 on the default http port, 80. The Zope server is assumed to be located on an internal machine with the IP address 192.168.1.1 and running on port 8080.

Virtual hosts are usually found in the Apache configuration file httpd.conf. You can read the complete documentation for virtual hosts on Apache's web site.

The first thing you want to add to your Apache configuration is a directive that tells the server that you are going to be using name based virtual hosts. This is done as follows:

   NameVirtualHost 120.1.1.1

This tells the Apache server that any virtual hosts specified with the IP address 120.1.1.1 will now be configured by the hostname that they were called with.

The next thing to do is to configure each named virtual host. The first example sets up a virtual host for yourdomain.com and www.yourdomain.com, both located at the IP address 120.1.1.1. Essentially, all hits are redirected to the Zope server. The L option tells the server that this is the last rule that should be applied (probably unnecessary here). The P option tells the server to use the proxy module in implementing this rule.

   <VirtualHost 120.1.1.1>
   DocumentRoot /home/httpd/html
   ServerName www.yourdomain.com
   ServerAlias yourdomain.com
   RewriteEngine On
   RewriteRule ^/(.*) http://192.168.1.1:8080/$1 [L,P]
   </VirtualHost>

The next virtual host is for the new hostname support.yourdomain.com. The content for this virtual host is located in the support directory of the Zope server.

   <VirtualHost 120.1.1.1>
   DocumentRoot /home/httpd/html
   ServerName support.yourdomain.com
   RewriteEngine On
   RewriteRule ^/(.*) http://192.168.1.1:8080/support/$1 [L,P]
   </VirtualHost>

You should now be able to go to http://support.yourdomain.com/ and the rewrite rule should redirect you to the support directory of your Zope server. The problem is that Zope doesn't know that it came from support.yourdomain.com and this will show in the URLs generated by Zope, so you have to tell it this information. This is described in the following section.

NOTE: There is no reason that you have to redirect requests to a different machine. The Zope server and the Apache server can exist on the same machine. In this case, you would simply replace the 192.168.1.1 with 120.1.1.1.

2.3. Configuring Zope for Proxy Operation

At this point, you should have the virtual hosts set up in Apache. If you look at any of the URLs generated by Zope for images, base tags, etc., they will start with http://192.168.1.1:8080/. This isn't very useful to web surfers trying to access your site. To get around this, you need to use the Zope product SiteAcess.

SiteAccess allows you to change the base URL used by Zope. The top level of your Zope server should have a SiteAccess instance with the following parameters:

Base

http://www.yourdomain.com

Path

/

The support directory should have a SiteAccess instance with these parameters.

Base

http://support.yourdomain.com

Path

/

NOTE: It is highly recommended that you read the documentation for SiteAccess on the Zope web site (http://www.zope.org). Misconfiguring this product can lock you out of the Zope content manager.

2.4. Complications in the Zope Management Interface

Depending on how your virtual hosts are configured you may run into problems when trying to manage objects through the Zope management interface. Before virtual hosting, you probably accessed the management interface through http://192.168.1.1:8080/manage. But now, with SiteAccess, all of the Zope generated links in the management interface begin with http://www.yourdomain.com. This will cause unnecessary hits on the external site when using the Zope content management interface.

To get around this naming problem, you can do one of the following:

  1. add entries for your virtual hosts in your /etc/hosts or c:\windows\hosts that point to your internal addresses (i.e. 192.168.1.1) rather than their real address on the external network (i.e. 120.1.1.1)

  2. if you have an internal DNS server, configure the virtual host names to point to their internal addresses

2.5. Delivering Static Content through Apache

While Zope is great for many things, delivering large files and static content are better handled by a plain old Apache server. Using some additional rewrite rules this is fairly simple to do in the virtual host configurations. Let's say that you have a lot of documentation on support.yourdomain.com contained in large PDF files accessible at the base URL http://support.yourdomain.com/pdf/. We'll assume that these PDF files are actually stored on the same machine as the Apache server in the directory /home/httpd/pdf/. To have these files delivered by the Apache server rather than Zope, add the following line to the support.yourdomain.com host configuration before the rewrite rule that redirects visitors to the Zope machine.

   RewriteRule ^/pdf - [L]

Alternatively, you can make a catch all rule that will redirect all URLs with the root "static" to the plain Apache server with the following rule.

   RewriteRule ^/static(.*) /home/httpd/html$1 [L]

3. Conclusion

The example virtual host in this document is fairly simple, but can get you a long way. There are many more advanced rewrite rules and virtual host paramaters that you can use to customize your virtual hosts beyond reason. If the examples given in this document don't quite get you the server configuration you want, you can read further documentation at www.apache.org and www.zope.org.

Copyright © 2000 theMorgue.org. All Rights Reserved.