Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add SSL to Rails App?

In my project, I just want to make my every request will be done through SSL. how to acheive this? I mean how to add SSL to Rails App?

I tried adding

   config.force_ssl = true

to application.rb. but It showing me an error like this.

Invalid request: Invalid HTTP format, parsing fails.

/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>'
like image 853
Kamesh Avatar asked Oct 01 '22 03:10

Kamesh


1 Answers

First you need to set ssl certificate to your Application. For example, if you're using nginx, then you need to make something like this nginx config

Use a filter in application_controller.rb or use some gem

class ApplicationController < ActionController::Base
  before_filter :redirect_to_https

  def redirect_to_https
    redirect_to :protocol => "https://" unless request.ssl?
  end
end
like image 178
user3493149 Avatar answered Oct 13 '22 11:10

user3493149