Category: Hosting

Nginx (and Puma behind) maintenance mode for Rack/Rails applications with Capistrano

Same for Apache + Passenger: Apache (Passenger) Maintenance mode for Ruby on Rails application with Capistrano

There is a time, when we need to switch our apps into maintenance mode. Maybe it is because of some data processing stuff, maybe because of backups, deployment or whatever good reason you might have. To be honest it doesn’t matter why. What does matter, is how we should handle working users of our apps. Even if you use a zero downtime deployment using Puma (great article on howto here), sometimes you just need to shutdown your app for few minutes.

Of course all the downtimes should take place when there is the smallest amount of users online. In most cases it might be a good idea to switch application off in the middle of the night (or on Sunday, etc.), but this won’t solve our primary problem: what should we show users that are already online?

The worst scenario ever would be showing them nothing (for example by shutting down whole application server). Users probably will think, that something bad happened. Much better idea is to show users a maintenance page with some sort of information like “Temporary down for maintenance”. It would be even better, if such a page would automatically display when needed.

In order to obtain this, we need to do two things:

  1. Configure our Nginx server block
  2. Add an additional Capistrano task

503 configuration for your Nginx server block (virtual host)

Add this at the end of your server block configuration (before the last curly bracket). It will test if a maintenance.txt file exists in a tmp directory, and if so, it will serve a 503 error page. It is worth pointing out, that all the assets will be served normally (we ignore them):

# Set a maintenance page
error_page 503 @maintenance;

location @maintenance {
  if (!-f $request_filename) {
    rewrite ^(.*)$ /503.html break;
  }
}

And in a location / section of the same file:

location / {
  # If we request a file that exists (assets/images/etc) serve them
  if (-f $request_filename) {
    break;
  }

  # If we request anything else - put 503 when needed
  if (-f $document_root/../tmp/maintenance.txt){
    return 503; 
  }

  # Here should go rest of this section
}

Capistrano hookup

To automate turning maintenance page on and off, I use a set of simple Capistrano tasks, enclosed in a Nginx namespace:

namespace :nginx do
  desc 'Switch current project into maintenance mode'
  task :lock do
    on primary :db do
      within release_path do
        execute :touch, 'tmp/maintenance.txt'
      end
    end
  end

  desc 'Turn off current project maintenance mode'
  task :unlock do
    on primary :db do
      within release_path do
        execute :rm, '-f tmp/maintenance.txt'
      end
    end
  end
end

Usage example (in my deploy.rb):

namespace :deploy do
  before :starting, 'nginx:lock'
  # Some other tasks...
  after :finished, 'nginx:unlock'
end

Good luck and as few maintenance downtime as possible!

Errbit + HTTPS: Setting up Errbit reporter (Airbrake v4 gem) to work with self-signed HTTPS Errbit

Warning: This post is outdated and this fix will work only with old Airbrake version (v4) - it does not work with the v4 version of Airbrake notifier. If you're looking for a solution for Airbrake v5, please refere to this post: Errbit + HTTPS: Setting up Errbit reporter (Airbrake v5 gem) to work with self-signed HTTPS certificate

Having an error catcher like Errbit behind SSL is generally a good idea. Especially when Errbit is hosted on a different server than you application (for example when you manage multiple apps with one Errbit instance). In many cases you will have a self-signed certificate (why would you pay for a cert for internal tool). If you try to use it with Airbrake, you will see following error:

2.1.0 :002 > Airbrake.notify Exception.new('test')
** [Airbrake] Unable to contact the Airbrake server. HTTP Error=SSL_connect 
   returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
** [Airbrake] Environment Info: [Ruby: 2.1.0] [Rails: 4.0.4] [Env: production]
** [Airbrake] Failure: NilClass
** [Airbrake] Environment Info: [Ruby: 2.1.0] [Rails: 4.0.4] [Env: production]
** [Airbrake] Notice details: 
  exception: test
  api_key: key
  backtrace: (irb):2:in `irb_binding'
/lib/ruby/2.1.0/irb/workspace.rb:86:in `eval'
/lib/ruby/2.1.0/irb/workspace.rb:86:in `evaluate'
/lib/ruby/2.1.0/irb/context.rb:380:in `evaluate'
/lib/ruby/2.1.0/irb.rb:492:in `block (2 levels) in eval_input'
/lib/ruby/2.1.0/irb.rb:624:in `signal_status'
/lib/ruby/2.1.0/irb.rb:489:in `block in eval_input'
/lib/ruby/2.1.0/irb/ruby-lex.rb:247:in `block (2 levels) in each_top_level_statement'
/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `loop'
/lib/ruby/2.1.0/irb/ruby-lex.rb:233:in `block in each_top_level_statement'
/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `catch'
/lib/ruby/2.1.0/irb/ruby-lex.rb:232:in `each_top_level_statement'
/lib/ruby/2.1.0/irb.rb:488:in `eval_input'
/lib/ruby/2.1.0/irb.rb:397:in `block in start'
/lib/ruby/2.1.0/irb.rb:396:in `catch'
/lib/ruby/2.1.0/irb.rb:396:in `start'
[GEM_ROOT]/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
[GEM_ROOT]/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
[GEM_ROOT]/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

In order to make it work you need to disable SSL verification for Ruby:

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

Of course keep in mind, that it will disable SSL verification for all other libs as well.

Whole Errbit config should look like this:

Airbrake.configure do |config|
  config.api_key = 'api_key'
  config.host    = 'errbit.domain'
  config.port    = 443
  config.secure  = true
  config.ignore_only = ['ActiveRecord::RecordNotFound']
end if Rails.env.production?

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

Copyright © 2025 Closer to Code

Theme by Anders NorenUp ↑