gpt4 book ai didi

ruby-on-rails - 如何将 SSL 添加到 Rails 应用程序?

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:15 25 4
gpt4 key购买 nike

在我的项目中,我只想让我的每个请求都通过 SSL 完成。如何实现这一点?我的意思是如何将 SSL 添加到 Rails 应用程序?

我尝试添加

   config.force_ssl = true

到application.rb。但它向我显示了这样的错误。

无效请求:HTTP格式无效,解析失败。

/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/request.rb:84:in `execute'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/request.rb:84:in `parse'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/connection.rb:41:in `receive_data'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/backends/base.rb:73:in `start'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/thin-1.6.1/lib/thin/server.rb:162:in `start'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands/server.rb:84:in `start'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in <top (required)>'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap'
/home/kamesh/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.2/lib/rails/commands.rb:71:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'

最佳答案

首先您需要为您的应用程序设置 ssl 证书。例如,如果你使用的是 nginx,那么你需要做这样的事情 nginx config

在 application_controller.rb 中使用过滤器或使用一些 gem

class ApplicationController < ActionController::Base
before_filter :redirect_to_https

def redirect_to_https
redirect_to :protocol => "https://" unless request.ssl?
end
end

关于ruby-on-rails - 如何将 SSL 添加到 Rails 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22831983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com