Multi-Site Mephisto Apache 2.2 Config

Mephisto truly is a wonderful piece of weblog/cms software. It's even more powerful in multi-site mode, where you can have completely separate blog instances running under a single installation. Doing this, however, requires a fair bit of RewriteRule magic in your Apache configuration. Crafting large sets of Apache rewrite rules is a very exacting science. One which I do not relish having to do more than once for a given set of behaviors. And so, to alleviate the pain for my future self and others, I present a full virtual host configuration suitable for use with Apache 2.2 and mod_proxy_balancer with a cluster of Mongrels.

This config is for two sites: example.com and blog.example.com. Because of the way Mephisto stores cached pages, all requests to www.example.com are redirected to example.com (no www). This is wise in general. It proxies requests to a cluster of three mongrels.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<VirtualHost *:80>
  ServerName www.example.com
  ServerAlias example.com
  ServerAlias blog.example.com

  # admin's email
  ServerAdmin awesome@example.com

  # paths
  DocumentRoot /var/www/example/current/public
  ErrorLog /var/www/example/current/log/error_log
  CustomLog /var/www/example/current/log/access_log combined

  ServerSignature On

  RewriteEngine On

  # no-www
  RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  RewriteRule (.*) http://example.com$1 [L,R=301]
  
  # Don't redirect /admin*. Send it directly to rails. 
  RewriteCond %{REQUEST_URI} ^/admin.* 
  RewriteRule ^/(.*)$ balancer://example_cluster%{REQUEST_URI} [P,QSA,L]

  # Basic content redirection
  RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/index.html -f
  RewriteRule ^/?$ /cache/%{HTTP_HOST}/index.html [QSA,L]
  
  RewriteCond %{REQUEST_URI} !.*/mephisto/.*
  RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/$1.html -f
  RewriteRule ^/([^.]+)$ /cache/%{HTTP_HOST}/$1.html [QSA,L]

  # asset redirection 
  RewriteCond %{REQUEST_FILENAME} /assets/.* 
  RewriteCond %{REQUEST_FILENAME} !-f 
  RewriteRule ^/(assets)(.*)$ /assets/%{HTTP_HOST}/$2 [QSA,L]

  # other redirection (imgs, js, css, ...) 
  RewriteCond %{REQUEST_FILENAME} !-f 
  RewriteCond %{REQUEST_URI} !.*/mephisto/.* 
  RewriteCond %{REQUEST_URI} !.*html 
  RewriteCond %{REQUEST_URI} !^/cache
  RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/$1 -f
  RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}/$1 [QSA,L]

  # Redirect all non-static requests to cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://example_cluster%{REQUEST_URI} [P,QSA,L]

  # Rewrite debugging
  # RewriteLogLevel 4
  # RewriteLog "/var/www/example/current/log/rewrite.log"
  
  # Error pages
  ErrorDocument 503 /503.html

  # Proxy setup
  <Proxy balancer://example_cluster>
    BalancerMember http://127.0.0.1:5000
    BalancerMember http://127.0.0.1:5001
    BalancerMember http://127.0.0.1:5002
  </Proxy>

  ProxyPreserveHost On
</VirtualHost>

Based loosely on the config from the Mephisto Wiki.


About this entry