This isn’t so much a blog post as somewhere convenient to put some information for ncl.rb.
My Apache 2.2 + modproxybalancer virtualhost config
<VirtualHost *:80>
ServerName yourapp.com
DocumentRoot /home/web/yourapp.com/current/public/
<Directory "/home/web/yourapp.com/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# Configure the cluster member proxy
<Proxy balancer://your_app_cluster>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMember http://127.0.0.1:3003
</Proxy>
RewriteEngine On
# If there is a maintenence.html file in your
# public dir all requests will get rerouted to
# this file. This is for use with capistrano
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [L]
# Rewrite index to check for static index.html
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached pages with .html extentions
RewriteRule ^([^.]+)$ $1.html [QSA]
# All dynamic requests get sent to the cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://your_app_cluster%{REQUEST_URI} [P,QSA,L]
# Deflate for clients that support it.
AddOutputFilterByType DEFLATE text/html text/plain text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Error and access logs.
ErrorLog /var/log/apache/app_error.log
CustomLog /var/log/apache/app_access.log combined
</VirtualHost>
Now if you take the above example it’ll send all requests to your pack of mongrels, but say you want to have more then one application running on your domain (say beast).
You’ll need to create another proxy balancer
<Proxy balancer://your_second_app_cluster>
BalancerMember http://127.0.0.1:3004
BalancerMember http://127.0.0.1:3005
BalancerMember http://127.0.0.1:3006
BalancerMember http://127.0.0.1:3007
</Proxy>
And then add the RewriteRule for it
RewriteRule ^/forums/(.*)$ balancer://your_second_app_cluster%{REQUEST_URI} [P,QSA,L]
Now anything going to / that isn’t /forums/.* will goto yourappcluster and anything with /forums/.* will goto yoursecondapp_cluster
<VirtualHost *:80>
ServerName yourapp.com
DocumentRoot /home/web/yourapp.com/current/public/
<Directory "/home/web/yourapp.com/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
# Configure the cluster member proxy
<Proxy balancer://your_app_cluster>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMember http://127.0.0.1:3003
</Proxy>
<Proxy balancer://your_second_app_cluster>
BalancerMember http://127.0.0.1:3004
BalancerMember http://127.0.0.1:3005
BalancerMember http://127.0.0.1:3006
BalancerMember http://127.0.0.1:3007
</Proxy>
RewriteEngine On
# If there is a maintenence.html file in your
# public dir all requests will get rerouted to
# this file. This is for use with capistrano
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /maintenance.html [L]
# Rewrite index to check for static index.html
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached pages with .html extentions
RewriteRule ^([^.]+)$ $1.html [QSA]
# All dynamic requests get sent to the cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/forums/(.*)$ balancer://your_second_app_cluster%{REQUEST_URI} [P,QSA,L]
RewriteRule ^/(.*)$ balancer://your_app_cluster%{REQUEST_URI} [P,QSA,L]
# Deflate for clients that support it.
AddOutputFilterByType DEFLATE text/html text/plain text/xml
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Error and access logs.
ErrorLog /var/log/apache/app_error.log
CustomLog /var/log/apache/app_access.log combined
</VirtualHost>
yourappcluster mongrel_cluster.yml
--- port: "3000" environment: production address: 127.0.0.1 pid_file: log/mongrel.pid servers: 4 yoursecondappcluster mongrelcluster.yml --- port: "3004" environment: production address: 127.0.0.1 pid_file: log/mongrel.pid servers: 4
Hope that made sense, as its off the top of the head apart from the base apache 2.2 vhost config
UPDATE If your running a Rails app under a path remember to set
ActionController::AbstractRequest.relative_url_root = '/path'
In your environment.rb or your links will be wrong
Vincent is a self-confessed geek, who's day job is as a Rails developer, outside of work he likes to play with home automation gadgets. He resides in Newcastle upon Tyne.