I recently moved a project to the latest & greatest in Rails deployment configurations, mod_rails. Overall I’ve pleased with simplicity of the setup, the memory savings and tools like passenger-memory-stats, especially for a small project on a not-too-beefy instance. With mod_rails comes Apache, which can do everything. But, perhaps for that exact reason, it can be more complicated to do the simple things than with lighty or nginx. The answer is often to use mod_rewrite which is like a Swiss Army knife – but have you ever tried to cut anything with the saw on that thing? Not so easy.

That is a long way of saying it took me longer than I thought it should have to figure out to proxy a certain request in Apache. I’m not an Apache expert, so maybe someone will point a better way of doing this (which would be great). Since it took me much googling and experimenting, I thought I’d post this for the next person to come along.

My setup has a main site running Rails on the primary domain, say “foo.com”, and a subdomain, “sub.foo.com”, that proxies to a backend server running Jetty. The responses from Jetty should go directly to the client. Here’s the section from my httpd.conf that makes it happen:

<VirtualHost *:80>
  # usual setup for Rails ...
  ServerName foo.com
  ServerAlias www.foo.com
  DocumentRoot /var/www/foo/current/public
  # Proxy requests to sub.foo.com to Jetty running locally
  # typically turned off for reverse proxy according to apache docs
  ProxyRequests Off
  # If turned off, url written in browser to "http://localhost:9091/blah"
  ProxyPreserveHost On
     Order deny,allow
     Allow from localhost

  RewriteEngine   on
  # Rewrite requests if domain matches. 
  RewriteCond %{HTTP_HOST} ^sub\.foo\.com$
  # Forward entire request URI to server. The "[P]" is for proxy!
  RewriteRule (.*) http://localhost:9091$1 [P]