zerosum dirt(nap)

evolution through a series of accidents

zerosum dirt(nap)

Moving to Nginx and Cap2.0

August 27, 2007 by nap · Comments

So a few months back I started using nginx on my staging server, front-ending for Mongrel, and just recently I’ve stated migrating some production stuff over to it. It’s pretty great as a lightweight Apache replacement. Incredibly simple syntax, very quick and close to the bone. Most of my production stuff still runs on Apache, but that may soon be changing. I also finally made the leap to Capistrano 2.0. Loving the new namespaced task hierarchy.

Anyway, here’s a simple alternative maintenance page recipe for Capistrano’s deploy:web:disable target and the corresponding Nginx config to make use of it. In case you’re unfamiliar with it, the disable web task basically redirects all requests to a maintenance page until deploy:web:enable is run, which returns things to normal. This recipe assumes you’ve created your own (static) maintenance.html page in public/maintenance.html and that it makes use of existing stylesheets and images — meaning that you don’t want to rewrite those requests.

in config/deploy.rb:

namespace :deploy do
  desc "Disable requests to the app, show maintenance page"
  web.task :disable, :roles => :web do
    run "cp #{current_path}/public/maintenance.html  #{shared_path}/system/maintenance.html"

  desc "Re-enable the web server by deleting any maintenance file"
  web.task :enable, :roles => :web do
    run "rm #{shared_path}/system/maintenance.html"

in nginx.conf (within your server block definition):

# allow requests for images, js, css, and icons to go through
# even if cap has been used to disable the site
if ($request_filename ~* /(images|javascripts|stylehseets)/) { break; }
if ($request_filename ~* \.ico$) { break; }

# for cap's deploy:web:disable task
if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html last;
blog comments powered by Disqus