SSL与Ruby on Rails
我需要做什么才能使流量到我的红宝石在rails应用程序使用https?我已经安装了证书,如果我在访问网站时在地址栏中手动输入“https://”,则会出现小锁图标,但只需手动在浏览器中访问www.example-app.com,就可以通过http发送流量://。SSL与Ruby on Rails
有一些单行配置还是比它更复杂?我以前从来没有必须处理SSL,所以如果我听起来像我不知道发生了什么,请原谅。
我在MediaTemple(gs)举办,如果有问题或任何人有这样的设置经验。
退房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
Ruby on Rails是一个应用程序框架,而不是一个Web服务器。您需要更改的HTTPS配置位于您的Web服务器(Apache,nginx等)配置中。
好吧,我知道那么多。我想我很好奇如何去改变这个设置。那会是什么?至少它的研究领导。我会围绕apache和mongrel配置文件进行探讨。 – 2010-01-23 01:35:49
你不需要改变杂种。只有Apache配置文件。我认为默认配置文件包含HTTPS配置,但默认情况下它是禁用的。另外看看使用nginx web服务器(使用更少的资源,更容易配置)。 – Zepplock 2010-01-23 01:57:36
这是很容易的,你并不需要一个宝石吧。我博客如何重定向没有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
,请参见发行说明等)
这不会在开发环境中造成问题吗?除非你也抽象出example.com部分? – jerhinesmith 2010-01-23 03:07:06
@jerhinesmith编辑,固定代码示例 – 2010-01-23 13:42:34
是否有request.request_uri更改的方法名?它没有在我的Ruby 1.9.3/Rails 3.2环境中定义。 – 2012-01-27 06:48:21
@AustinFitzpatrick你的链接与SSL完全无关 – lulalala 2013-07-31 10:34:28
是的,它看起来好像知识库已经改变了,不是。我会删除它。 – 2013-08-01 16:34:56