12 January 2010

Nginx Development Script for Passenger

Like most Rails developers, I incessantly tweak my development environment in pursuit of Nirvana. But like biological evolution, it’s not always an upward trend.

I recently jumped onto the Unicorn1 bandwagon. It starts quickly and subjectively serves pages faster even in development (as does every technical modification powered by hope). But it can’t compete with Phusion Passenger when serving multiple assets simultaneously2. Since many of my applications use dynamically generated graphics (or at least dynamic CSS with SASS), this can make a big difference.

But even with the minimal configuration needed to create a new Passenger app, it’s inconvenient for spontaneous apps or small demos. If I want to try out a Rails 3 application with Passenger, I don’t want to configure a new virtual host for just a few minutes of experimentation.

At RailsCamp 6, PeepCode author Pat Allan showed a beautiful solution. It’s a script that starts Nginx from any directory3 using a shared configuration file. I modified it ever so slightly and posted a screencast above with a walkthrough.

I store the script in my ~/bin directory so it can be called from any location:

I use the following configuration file, stored in my source-controlled dotfiles directory.

Now that I’m back with Passenger for development, both visual modifications and backend code tweaks happen much faster. And I can fire up the server in any directory.

1 Unicorn is a close-to-the metal Unix-based webserver written in Ruby.

2 Phusion Passenger for Apache or Nginx manages multiple servers when more requests come in than can be handled. The result is faster delivery of assets to the client without the need to manually manage application server instances.

3 Pat’s original article shows how to build Nginx from source.