You are not logged in Log in Join
You are here: Home » Members » jeffsasmor » Zope Virtual Hosting for Apache 2 » howto_view

Log in
Name

Password

 

Zope Virtual Hosting for Apache 2

 

Created by jeffsasmor . Last modified 2005-03-03 20:13:41.

Zope Virtual Hosting for Apache 2, a different approach using mapping files

(version 1.1, 3 March 2005)

I was trying to handle virtual hosting a little differently and came up with something others may find useful. I use a double mapping to control the Apache/VHM/Zope interface.

Doing so allows you to more finely control what sites map to folders, and do so without having to touch httpd.conf as you add or delete domains (of course, you do have to restart Apache). Also allows you to have a site in the Zope root.

I don't know if anyone else will find this useful, but here goes:

 In the Apache httpd.conf:

 <VirtualHost 11.22.33.44>
  RewriteEngine On
  RewriteMap lowercase int:tolower
  RewriteMap vhostmatch txt:/etc/httpd/conf/vhostmatch.map
  RewriteMap vhostRW txt:/etc/httpd/conf/vhostRW.map

  RewriteCond ${vhostmatch:${lowercase:%{SERVER_NAME}}} ^(/.*)$
  RewriteRule ^/(.*) http://localhost:8080/VirtualHostBase/http/%{HTTP_HOST}:80/${vhostRW:${lowercase:%{SERVER_NAME}}}/VirtualHostRoot/$1 [L,P]

 </VirtualHost>

Note that the second RewriteRule is all on one line. It gets executed if the preceding RewriteCond statement is true. The RewriteCond match is true if the lower-cased server name has a key:value pair in vhostmatch.map. SERVER_NAME is the xxx part of http://xxx.xxx.xxx (see cgi spec ). So the xxx.xxx.xxx part has to match a key in the vhostmatch map - i.e., the value is fetched using SERVER_NAME as the key. The value fetched has to match ^(/.)$ (the URL up to the query string, if any).

Then the RewriteRule gets the folder name from the vhostRW mapping (again using SERVER_NAME as the key) and sends it along to the Virtual Host Monster.

To support this, you of course need the two map files in the filesystem, e.g., /etc/httpd/conf/ but can be anywhere.

vhostmatch.map is used to provide a mapping between domain names and zope paths, for example:

 www.domain.com /
 domain.com /
 www.domain2.com /
 domain2.com /
 www.domain3.com /domain3
 domain3.com /domain 3

This maps (www.)domain.com and (www.)domain2.com to the zope root, and (www.)domain3 to zope folder /domain3 (i.e., a folder within the zope root folder). The duplication of names is needed to ensure that apache sees both the domain and the www subdomain. If all you want is a response to www then you can leave the other entry out.

vhostRW.map does a lookup to see what folder name to provide to the Virtual Host Monster. You only need to have entries in this mapping for folders, e.g.:

 www.domain3.com domain3
 domain3.com domain3

So, for this example, domain.com and domain2.com will render index.html in the Zope root. domain3.com will render the index.html in the domain3 folder.

Note that a subdomain that doesn't exist in the .map files will bypass Zope entirely and the browser will see whatever is in the so-called default server (e.g., /var/www/; often displays the Apache docs). There's probably some way around this; just haven't got into it yet as it doesn't matter to me personally just now. Actually, it's handy: I use it to keep static content out of Zope and in the filesystem. For example, you can reach me at ptang.com (an index_html in the Zope instance's root folder). But x.ptang.com will give you a go away page; x.ptang.com/manual gets you the Apache manual that just happened to be on my server. These latter two are in the default server's base directory of /var/www. Any subdomain that isn't in the .map files will work, too.

But this side effect is part of an inherent support for support for subdomains. If you have, for example:

 subdomain.domain.com /subdomain  (in vhostmatch.map)
 subdomain.domain.com subdomain   (in vhostRW.map)

Then http://subdomain.domain.com will render the subdomain folder in the Zope root.

Note: Using mappings does not slow Apache down, as the mappings are read in when Apache is restarted (according to the Apache docs). You of course need to restart Apache after making changes to either .map file just as you do for httpd.conf itself.

I don't particularly suggest that this is the best way to do any of this stuff, it just worked for me and may provide some help to someone else.

The inspiration for all this comes from the last example of one of the Apache manual pages on virtual hosting