Wednesday, February 01, 2006

Deploying Rails with Apache 2

This is critical to running rails behind apache

Deploying Rails with Apache 2
Set up the Apache Proxy Directives After you have Lighty set up and serving your application on its non-standard port, it's time to proxy passthrough connections to your Apache server to your Lighty-based Rails application. To do this, add some Proxy directives to your Apache configuration file, either to the main configuration or to a virtual host block. Here's a sample from one of my own running servers (with host names modified): ServerName mycoolapp.domain.com ProxyPass /svn ! ProxyPass / http://mycoolapp.domain.com:81/ ProxyPassReverse / http://mycoolapp.domain.com:81 ProxyPreserveHost on DAV svn SVNPath /Library/Subversion The first line tells Apache not to proxy requests for Subversion. The second maps in the URL namespace of the Lighty-based Rails application into the root of the namespace of the Apache-based server. This means that when a browser makes a request to http://mycoolapp.domain.com/noun/verb, Apache will proxy that request to http://mycoolapp.domain.com:81/noun/verb which will be handled by Lighty and therefore your Rails application. Since Apache is acting as a transparent reverse proxy, the user never sees this bit of magic. The third directive adjusts the Location and other headers to make sure that redirects from the back-end Rails application don't bypass the front-end Apache server. The last line preserves the Host header from the client. With this configuration, your Rails-based app will work correctly, but there are a few things to keep in mind. The first is that if you programmatically build up URL strings to your server, be sure to use your public server as the basis for them. Don't create URLs to your proxy server instead of your front-end server. The second is that some of the information about the original client can be accessed in the request environment variables, such as: * HTTP_X_FORWARDED_HOST * HTTP_X_FORWARDED_SERVER * HTTP_X_FORWARDED_FOR It's easy enough to serve up multiple Rails apps from a single Apache installation. Just set up multiple Virtual Host blocks in both your Apache and Lighty configurations and you'll be off to the races. For example, for the Apache configuration above, your Lighty configuration could have a virtual host block like this: $HTTP["host"] == "mycoolapp.domain.com" { server.document-root = var.mycoolapp + "/current/public" url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" ) server.error-handler-404 = "/dispatch.fcgi" fastcgi.server = ( ".fcgi" => ( "localhost" => ( "min-procs" => 2, "max-procs" => 2, "socket" => "/tmp/mycoolapp.fcgi.socket", "bin-path" => var.mycoolapp + "/current/public/dispatch.fcgi", "bin-environment" => ( "RAILS_ENV" => "production" ) ) ) ) } One last thing to note. In your Lighty log files, all of the requests will be logged as originating from your front end Apache proxy server. This is accurate, because the connections are coming from your Apache front-end server, but it may not be what you want. If, instead, you want to log the IP address from which the request originally came from, you can set up your Lighty access log format using the X-Forwarded-For header. Here's an example: accesslog.format = "%{X-Forwarded-For}i %v %u ..."

No comments: