2010-01-23 145 views
12

我需要做什麼才能使流量到我的紅寶石在rails應用程序使用https?我已經安裝了證書,如果我在訪問網站時在地址欄中手動輸入「https://」,則會出現小鎖圖標,但只需手動在瀏覽器中訪問www.example-app.com,就可以通過http發送流量://。SSL與Ruby on Rails

有一些單行配置還是比它更復雜?我以前從來沒有必須處理SSL,所以如果我聽起來像我不知道發生了什麼,請原諒。

我在MediaTemple(gs)舉辦,如果有問題或任何人有這樣的設置經驗。

回答

14

退房ssl_requirement寶石。

它可以讓你在你的控制器哪些動作可以通過https和行動可以可以通過https指定。然後它將負責將http重定向到https,反之亦然。

從文檔:

class ApplicationController < ActiveRecord::Base 
    include SslRequirement 
end 

class AccountController < ApplicationController 
    ssl_required :signup, :payment 
    ssl_allowed :index 

    def signup 
    # Non-SSL access will be redirected to SSL 
    end 

    def payment 
    # Non-SSL access will be redirected to SSL 
    end 

    def index 
    # This action will work either with or without SSL 
    end 

    def other 
    # SSL access will be redirected to non-SSL 
    end 
end 
+0

@AustinFitzpatrick你的鏈接與SSL完全無關 – lulalala 2013-07-31 10:34:28

+0

是的,它看起來好像知識庫已經改變了,不是。我會刪除它。 – 2013-08-01 16:34:56

6

Ruby on Rails是一個應用程序框架,而不是一個Web服務器。您需要更改的HTTPS配置位於您的Web服務器(Apache,nginx等)配置中。

+0

好吧,我知道那麼多。我想我很好奇如何去改變這個設置。那會是什麼?至少它的研究領導。我會圍繞apache和mongrel配置文件進行探討。 – 2010-01-23 01:35:49

+0

你不需要改變雜種。只有Apache配置文件。我認爲默認配置文件包含HTTPS配置,但默認情況下它是禁用的。另外看看使用nginx web服務器(使用更少的資源,更容易配置)。 – Zepplock 2010-01-23 01:57:36

3

這是很容易的,你並不需要一個寶石吧。我博客如何重定向沒有www在軌道here。重定向到https(幾乎)完全一樣。

class ApplicationController < ActionController::Base 
    before_filter :redirect_to_https 

    def redirect_to_https 
    redirect_to "https://example.com#{request.fullpath}" if !request.ssl? && request.host != "localhost" 
    end 
end 

將您的before_filter應用於任何要確保保留在SSL安全之後的任何內容。我通常是代碼重用和寶石之一,但這是一個非常簡單的方法。閱讀更多關於request.protocol。 (請注意,在Ruby 1.9.3/Rails的3.2環境下,名稱爲request.fullpath;在一些早期版本中,request.request_uri,請參見發行說明等)

+0

這不會在開發環境中造成問題嗎?除非你也抽象出example.com部分? – jerhinesmith 2010-01-23 03:07:06

+0

@jerhinesmith編輯,固定代碼示例 – 2010-01-23 13:42:34

+0

是否有request.request_uri更改的方法名?它沒有在我的Ruby 1.9.3/Rails 3.2環境中定義。 – 2012-01-27 06:48:21