Logo Devoh

Supplanting Lighttpd and Mongrel with Nginx and Thin

I've been using lighttpd and Mongrel to deploy my Rails applications for some time now. While I've kept abreast of the other deployment options, I've never really had a need for a more optimal setup, but lately I've started feeling that it's now time to move on to something that's more actively maintained. With my most recent deployment I decided to go ahead and mix things up a bit and try out Nginx and Thin.

I found this choice of servers to be trivial to configure, and I especially like having the ability to use UNIX domain sockets to avoid the overhead of using TCP sockets. I also found Thin's built-in init script installation a breath of fresh air compared to mongrel_cluster's manual configuration process. Even setting up SSL in Nginx proved to be a fairly simple procedure, except for one gotcha in getting the ssl_requirement plugin working. I had to add a X-Forwarded-Proto header to let Rails know that SSL was turned on. In the Nginx server block for the secure site, the location headers should look like this:

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto https;
  proxy_set_header Host $http_host;
  proxy_redirect false;
  # …
}

The only other part that required some custom configuration was letting Capistrano know how to start and stop Thin. I ended up with this simple addition to my deploy.rb:

namespace :deploy do
  %w(start stop restart).each do |action|
  desc "#{action.capitalize} the application servers."
    task action.to_sym, :roles => :app do
      run "thin #{action} -C /etc/thin/#{application}.yml"
    end
  end
end

I'm happy with the way this little experiment turned out—happy enough that I'll probably stick with this combination for a while.