2013-05-02 46 views
0

如何將www.example.com重定向到https://www.example.com在開發中,我如何使用自簽名證書重定向到https?

我正在開發中,我正在使用自簽名證書。我在application.rb中加入config.force_ssl = true和我的應用程序控制器添加了這種方法的before_filter回調:

def redirect_secure 
    redirect_to protocol: "https" if request.protocol == "http" 
end 
+0

的可能重複[你是如何處理的發展SSL?(http://stackoverflow.com/questions/2118685/how-do-you-handle-ssl-在開發中) – 2013-05-02 10:27:13

回答

1

對於服務器的SSL證書配置發展添加以下代碼腳本/軌文件,

module Rails 
    class Server < ::Rack::Server 
     def default_options 
      super.merge({ 
       :Port => 3445, 
       :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("/home/mohanraj/myCA/server_key.pem").read), 
       :SSLCertificate => OpenSSL::X509::Certificate.new(
         File.open("/home/mohanraj/myCA/server_crt.pem").read), 
       :SSLCertName => [["CN", WEBrick::Utils::getservername]] 
      }) 
     end 
    end 
end 

注意:請爲ssl​​文件提供正確的路徑。

請點擊此鏈接重定向http://www.railway.at/2013/02/12/using-ssl-in-your-local-rails-environment/

+0

感謝您的回答!我會試試這個。 – MoMo 2013-05-02 10:16:19

+0

Monkeypatching ActionController :: ForceSSL是解決這個問題的最後手段?我讀過這篇文章:https://github.com/rails/rails/pull/5023,開發人員說,在任何環境下,默認情況下都不應禁用SSL。任何機會,任何人都知道這個請求是否有更新? – MoMo 2013-05-03 03:10:34