我正在努力为 Heroku(欧盟地区)上运行的 Rails 应用程序强制实施 SSL 连接。
我已成功将证书(包括 Heroku SSL 附加组件)添加到自定义域(如果我明确声明 https://..com 则效果完美)
但我想将所有 http 请求重定向到 https 连接。
对于美国地区的应用程序,这需要将自定义域 DNS 记录指向 .herokussl.com 而不是 .herokuapp.com。文件:https://devcenter.heroku.com/articles/ssl-endpoint#dns-and-domain-configuration https://devcenter.heroku.com/articles/ssl-endpoint#dns-and-domain-configuration
对于欧盟地区的应用程序,自定义 DNS 记录仍应指向 .herokuapp.com,默认情况下它似乎不会强制执行 SSL 连接。
因此,我的问题是:如何强制与在欧洲运行的 Heroku 应用程序的所有连接都通过 SSL 运行?
生产.rb
Rails.application.configure do
config.force_ssl = true
end
这会将所有 http 流量重定向到 https
编辑:值得注意的是,这是 Rails 的东西,而不是 heroku 的东西。
修订:
由于这个答案/问题经常被看到和投票,因此也可以在控制器中基于每个请求:
class AccountsController < ApplicationController
force_ssl if: :ssl_configured?
def ssl_configured?
!Rails.env.development?
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)