Rails and ProxyPreserveHost

22 Feb 2008

This will probably be a "duh" thing for the Apache gurus out there, but it was a learning experience for me. The other day I ran into a little Apache/Rails problem. I had a front end server running Apache, and a back end server running Apache and proxying to a Mongrel cluster. When the Rails app controller code would call do_redirect the client browser would be redirected to the back end server's hostname. The solution was two-fold:

  • I added a ProxyPreserveHost On directive to the front end server
  • I added a ServerAlias frontend directive on the backend server to the definition for the virtual host that I was proxying to. This might not have been necessary if we only had one vhost on the backend, or if we had no vhosts on the backend and were just proxying to an IP address. But we had multiple vhosts on that backend machine, so, bob's your uncle.

Once those directives were in place the redirects worked as expected and all was well. Apache for the win!