使用Ruby on Rails进行SSL

我需要做些什么才能让我的ruby在rails应用程序上获得流量以使用https? 我安装了证书,如果我在访问网站时在地址栏中手动输入“https://”,则会出现小锁图标,但只需在浏览器中手动转到www.example-app.com即可通过http发送流量://。

是否有一些单行配置还是比它更复杂? 我以前从来没有处理过SSL,所以请原谅我听起来我不知道发生了什么。

我正在MediaTemple中托管(gs),如果这很重要或任何人都有这样的设置经验。

查看ssl_requirement gem。

它允许您在控制器中指定通过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 

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

这很简单,你不需要gem。 我在博客中写了如何在没有www情况下重定向。 重定向到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安全性之后的任何内容。 我通常是一个代码重用和gem,但这个非常简单。 阅读有关request.protocol的更多信息。 (注意,在Ruby 1.9.3 / Rails 3.2环境中,名称是request.fullpath ;在某些早期版本中,它是request.request_uri ;请参阅发行说明等)

https://github.com/bartt/ssl_requirement这里是ssl_requirement的更新版本。