简体   繁体   English

强制Ruby on Rails应用加载HTTPS版本

[英]Force Ruby on Rails app to load on HTTPS version

I have an HTTP and HTTPS version of my Ruby on Rails running, and if someone accesses through the HTTP one, I want to automatically reload the HTTPS version. 我正在运行Ruby on Rails的HTTP和HTTPS版本,如果有人通过HTTP访问,我想自动重新加载HTTPS版本。

I have done this before on Apache with .httaccess. 我之前在.httaccess的Apache上完成了此操作。 How can I do it on Rails? 如何在Rails上做?

Thanks 谢谢

在您的production.rb文件中(或您要强制ssl的任何环境中)添加

config.force_ssl = true

You can achieve it by putting the below code in config/applcation.rb 您可以通过将以下代码放在config/applcation.rb来实现它

#config/application.rb
config.force_ssl = true

Check this blog and this SO post for more info. 检查此博客和此SO帖子以获取更多信息。

You can also achieve that by: 您还可以通过以下方法实现这一目标:

class ApplicationController < ActionController::Base
  force_ssl if: :ssl_enabled?

  private

  def ssl_enabled?
    %w(staging production).include?(Rails.env)
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM