2010-08-30 23 views
6

有沒有辦法將SSL選項傳遞到「rails server」(在Rails 3.0.0上),使用自定義Rack配置或類似的東西?我試圖做兩件事情:如何將SSL選項傳遞到Rails 3.0中的「rails server」?

  1. 使黃瓜運行涉及安全和非安全網址的測試,並
  2. 使事情變得簡單了新的開發者,所以他們沒有成立Apache和配置所有的SSL /證書的東西,甚至可以寫一行代碼。

在2.3.8上我們有一個分叉的腳本/服務器,它可以在第二個端口上啓動一個特殊的WEBrick,並帶有所有適當的SSL選項。當我嘗試升級到Rails 3時,當然會爆炸,所以我試圖弄清楚如何解決這個問題,理想的做法是不需要分叉任何東西。

在我們的分叉腳本/服務器我們設置選項如下所示:

:SSLEnable  => true, 
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE, 
:SSLPrivateKey  => OpenSSL::PKey::RSA.new(File.open(current_dir + "/config/certs/server.key").read), 
:SSLCertificate   => OpenSSL::X509::Certificate.new(File.open(current_dir + "/config/certs/server.crt").read), 
:SSLCertName => [ [ "CN", WEBrick::Utils::getservername ] ] 

,但我不知道該怎麼做,在新的框架。

感謝您的幫助!

回答

5

查看Thin服務器來代替WEBrick。使用Thin有很多好處,我不能在這裏列出它們,但它應該解決您的問題,因爲它支持SSL。

當開始thin,通過以下選項:

SSL options: 
    --ssl      Enables SSL 
    --ssl-key-file PATH   Path to private key 
    --ssl-cert-file PATH   Path to certificate 
    --ssl-verify     Enables SSL certificate verification 

在生產中,你會非常想在Nginx的或Apache層來處理SSL,但是這應該處理您的開發需求。

1

這是我想出的解決方案。我修改script/rails看起來像這樣:

#!/usr/bin/env ruby 
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. 

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

# Hack our SSL certs into Thin TcpServer, only in development environment 
require 'thin' 
module Thin 
    module Backends 
    TcpServer.class_eval do 
     def initialize_with_SSL(host, port) 
     if Rails.env.development? 
      Rails.logger.info "Loading SSL certs from ./ssl_dev..." 
      @ssl = true 
      @ssl_options = { 
      :private_key_file => File.expand_path("../../ssl_dev/server.key", __FILE__), 
      :cert_chain_file => File.expand_path("../../ssl_dev/server.crt", __FILE__), 
      :verify_peer => nil 
      } 
     end 

     initialize_without_SSL(host, port) 
     end 

     alias_method :initialize_without_SSL, :initialize 
     alias_method :initialize, :initialize_with_SSL  
    end 
    end 
end 

# Must load 'rails/commands' after Thin SSL hack 
require 'rails/commands' 
相關問題