Ruby on Rails
HowtoSetupApacheProxyingToLighttpdWithFastCGI

This howto describes how to setup an Apache server to proxy for local Lighttpd instances. The advantages are obvious:

Configure Apache

Configure your Apache to use mod_proxy
Add at the bottom of your Apache configuration file:


ProxyPass /myrailsapp http://localhost:3000/myrailsapp
ProxyPassReverse /myrailsapp http://localhost:3000/myrailsapp

For any more configurations add similar lines, like:

ProxyPass /myrailsapp2 <a href="http://localhost">http://localhost</a>:3001/myrailsapp2
ProxyPassReverse /myrailsapp2 <a href="http://localhost">http://localhost</a>:3001/myrailsapp2

Configure Lighttpd

We assume that the Rails project is located at /home/someuser/somepath/myrailsapp.
Put the following in config/lighttpd.conf:

  1. Start using ./script/server lighttpd

server.port = 3000

server.modules = ( “mod_rewrite”, “mod_accesslog”, “mod_fastcgi” , “mod_alias” )
server.document-root = “/home/someuser/somepath/myrailsapp/public”
server.event-handler = “freebsd-kqueue”

  1. this is for FreeBSD!
    server.indexfiles = ( “index.html” , “dispatch.fcgi” )

$HTTP“url” =~ “^/myrailsapp” {
server.document-root = “/home/someuser/somepath/myrailsapp/public”
alias.url = ( “/myrailsapp” => “/home/someuser/somepath/myrailsapp/public” )
accesslog.filename = “/home/someuser/somepath/myrailsapp/log/access.log”
server.error-handler-404 = “dispatch.fcgi”
server.errorlog = “/home/someuser/somepath/myrailsapp/log/lighttpd.error.log”
server.indexfiles = ( “index.html” , “dispatch.fcgi” )
# rails stuff
fastcgi.server = ( “.fcgi” =>
(
(
“socket” => “/home/someuser/somepath/myrailsapp/log/code.socket”,
“min-procs” => 2,
“max-procs” => 2,
“bin-path” => “/home/someuser/somepath/myrailsapp/public/dispatch.fcgi”,
“bin-environment” => ( “RAILS_ENV” => “development” )
)))
}

mimetype.assign = (
“.css” => “text/css”,
“.gif” => “image/gif”,
“.htm” => “text/html”,
“.html” => “text/html”,
“.jpeg” => “image/jpeg”,
“.jpg” => “image/jpeg”,
“.js” => “text/javascript”,
“.png” => “image/png”,
“.swf” => “application/x-shockwave-flash”,
“.txt” => “text/plain”
)

If you have more than one virtual site but keeping all your sockets in one place (like /tmp), you may need to use a different socket filename for each site, so code.socket above may become codea.socket for site1, codeb.socket for site2, etc.

If you want to have more debugging support add this to your lighttpd.conf at the bottom:

## enable debugging
debug.log-request-header   = "enable"
debug.log-response-header  = "enable"
debug.log-request-handling = "enable"
debug.log-file-not-found   = "enable"

And in config/environment.rb add at the bottom:

  1. Include your application configuration below
    ActionController::AbstractRequest.relative_url_root = ‘/myrailsapp’

A small note for anyone using Apache 2:
it appears you need to specify a rewrite rule rather than just rely on ProxyPass. The difference between the two is elegantly and succinctly put below (a direct paste from http://www.brainspl.at/articles/trackback/9 )

Apache 1.3.x:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias <a href="http://www.example.com">www.example.com</a>
    ProxyPass / <a href="http://example.com">http://example.com</a>:8000/
    ProxyPassReverse / <a href="http://example.com">http://example.com</a>:8000/
</VirtualHost>


Apache 2.x:

<VirtualHost *:80>
    ServerAdmin                 <a href="mailto:webmaster@username.tld">webmaster@username.tld</a>
        ServerName              example.com:80
        ProxyRequests           Off
        ProxyPreserveHost       On
        RewriteEngine           On
        RewriteRule             ^/(.*) <a href="http://127.0.0.1">http://127.0.0.1</a>:3000/$1 [P,L]
        ProxyPassReverse        / <a href="http://127.0.0.1">http://127.0.0.1</a>:3000/
</VirtualHost>

Reader comment: There are some problems with characters if you cut and paste the config text from this page. I got a smart quote for some double quotes and also the left bracket disappeared after the $HTTP. I don’t know if the wiki rendering is causing this. Can somebody please fix.