如何在Rails中配置WEBrick使用SSL?

32

在Rails 3之前,您可以修改script/server文件以添加SSL参数,并告诉服务器命令使用WEBrick的HTTPS版本。现在所有这些脚本都已经消失了,有人知道如何在Rails 3或4中使它们工作吗?


1
我已经找到了解决这个问题的方法,并在这里进行了记录。 - Chris
这里有一个对于Rails 2有用的链接: http://lists.rubyonrails.org/pipermail/rails/2006-January/012432.html - Chris
2
不要忘记更改URL并手动添加https://,否则您会收到奇怪的错误“ERROR OpenSSL :: SSL :: SSLError:SSL_accept returned = 1 errno = 0 state = SSLv2 / v3 read client hello A:http request” - Chloe
1
答案的详细信息应作为答案的一部分出现/复制在其中。如果其他网站崩溃,此答案将变得无用。 - Mathieu J.
我很好奇,你为什么想这样做? - bbozo
显示剩余3条评论
2个回答

28
在Rails 4中,scripts目录已经不存在,但是bin目录仍然存在。你可以通过编辑bin/rails脚本来让WEBrick使用SSL证书。这个方法在安装了rbenv的Rails 4和Ruby 2.1.1上进行了测试。这些内容来自这篇博客文章这个Stack Overflow问题
#!/usr/bin/env ruby

require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'

if ENV['SSL'] == "true"
  module Rails
      class Server < ::Rack::Server
          def default_options
              super.merge({
                  :Port => 3001,
                  :environment => (ENV['RAILS_ENV'] || "development").dup,
                  :daemonize => false,
                  :debugger => false,
                  :pid => File.expand_path("tmp/pids/server.pid"),
                  :config => File.expand_path("config.ru"),
                  :SSLEnable => true,
                  :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
                  :SSLPrivateKey => OpenSSL::PKey::RSA.new(
                                   File.open("certs/server.key").read),
                  :SSLCertificate => OpenSSL::X509::Certificate.new(
                                   File.open("certs/server.crt").read),
                  :SSLCertName => [["CN", WEBrick::Utils::getservername]],
              })
          end
      end
  end
end

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'

如果将SSL环境变量设置为true,从应用程序目录启动rails服务器现在可以启动启用了SSL的服务器。当省略该环境变量时,会保留默认的rails设置。

$ SSL=true rails s
=> Booting WEBrick
=> Rails 4.1.0 application starting in development on https://0.0.0.0:3001
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-04-24 22:59:10] INFO  WEBrick 1.3.1
[2014-04-24 22:59:10] INFO  ruby 2.1.1 (2014-02-24) [x86_64-darwin13.0]
[2014-04-24 22:59:10] INFO  
Certificate:
    Data:
...
如果您不想使用预生成的证书,可以使用WEBrick的Utils :: create_self_signed_cert,如此答案中所述: 配置WEBrick以使用自动生成的自签名SSL / HTTPS证书

它创建了一个问题,网站不在http上运行,甚至没有重定向到https。我已经启用了ssl.enforce=true,但它没有起作用。 - Dinesh Saini
我遇到了 bin/rails:3:in 'require': cannot load such file -- rails/commands/server (LoadError) 错误。 - Chandler Klüser

22

WEBrick的SSL/HTTPS替代方案:Thin上的SSL/HTTPS

如果您想为Rails应用程序设置HTTPS/SSL,但不想尝试使用WEBrick,可以尝试切换到使用Thin服务器,因为它提供了方便的选项来开箱即用地设置HTTPS/SSL。

安装Thin

首先,在Gemfile中添加Thin作为gem:

gem 'thin'

然后在命令行中运行bundle install

在开发环境中使用Thin HTTPS/SSL

如果您只想在本地开发环境中测试Rails应用程序的HTTPS/SSL功能,则只需运行以下命令:

thin start --ssl

我必须强调,这不适用于生产环境,因为您需要使用来自证书颁发机构的有效SSL证书,以便SSL / HTTPS连接可验证和安全。

其他选项

还有其他选项可以传递给Thin。您可以通过运行thin --help获取完整列表。例如,我喜欢指定自己的IP地址和端口,以及将Thin守护进程化为后台进程:

thin start --ssl \
  --address <ip-address> \
  --port <port> \
  --daemonize

使用 SSL 证书的 Thin HTTPS/SSL

如果您想让 Thin 使用 SSL 证书(例如,您从有效的证书颁发机构获得的证书),则可以使用以下选项:

thin start --ssl \
  --ssl-cert-file <path-to-public-certificate> \
  --ssl-key-file <path-to-private-key>

如果您有中间证书,则将其放入与公共证书相同的文件中,放在终端实体证书之后。 - mcr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接