Here's my solution which is compatible with both Rails 3 and 4.
Add the following code at the top of bin/rails
:
require 'rubygems'
require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'
module Rails
class Server < ::Rack::Server
def default_options
ssl_default_options = {
:Port => 443,
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open(File.expand_path('../../config/cert/server_development.key', __FILE__)).read),
:SSLCertificate => OpenSSL::X509::Certificate.new(File.open(File.expand_path('../../config/cert/server_development.crt', __FILE__)).read),
:SSLCertName => [['CN', WEBrick::Utils::getservername]]
}
ENV['RAILS_SSL'] ? super.merge(ssl_default_options) : super
end
end
end
You obviously need to generate a self signed certificate or buy one.
Then when you want to start WebRick from command line use RAILS_SSL=true rails s
. Usually you need sudo
to listen on port 443 so you may want to append -p 3001
to change port.