配置WEBrick以在Rails中使用SSL 4

我有一个Rails应用程序,我想在SharePoint 2013上部署。

为了实现某种身份validation方法,我需要WEBrick serverssl https提供服务,并在port https://localhost:3001上侦听传入的https。 不幸的是,我在配置服务器方面不是很有经验。

我只发现了一些旧版Rails版本的过时教程,似乎不再能完成这项工作了。

任何提示都非常感谢。

我知道你要求WEBrick,请原谅我提出别的建议,但我真的建议你使用Thin ruby web服务器(更快更轻),这样你也可以启动SSL并满足你的要求:

 $ thin start --ssl -p 3001 

只是不要忘记在你的Gemfile中添加gem’lema 😉

这是我的解决方案,它兼容Rails 3和4。

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 

您显然需要生成自签名证书或购买一个。

然后,当您想从命令行启动WebRick时,请使用RAILS_SSL=true rails s 。 通常你需要sudo来侦听端口443,所以你可能想要附加-p 3001来改变端口。