19

I have a Rails App that I want to deploy on SharePoint 2013.

In order to achieve some means of authentication, I need the WEBrick server to serve ssl https and listen to incoming https on port https://localhost:3001. Unfortunately, I am not very experienced with configuring the server.

I've have only found some outdated tutorials for older Rails version, that don't seem to do the job anymore.

Any tips are greatly appreciated.

Chloe
  • 25,162
  • 40
  • 190
  • 357
VikingR
  • 301
  • 1
  • 3
  • 8
  • Possible duplicate of [How do you configure WEBrick to use SSL in Rails 3?](http://stackoverflow.com/questions/3640993/how-do-you-configure-webrick-to-use-ssl-in-rails-3). Voted to close because the solution is basically identical to the accepted solution in that question. Not sure if the other one should actually be made a duplicate of this newer one or not. –  Mar 04 '14 at 04:23
  • @Cupcake That is for Rails 3, and Rails 4 doesn't use script\rails file. – Chloe Apr 17 '14 at 06:09
  • 1
    @Chloe you can't tell because you don't have enough reputation yet, but [the answer that used to be here but was subsequently deleted](http://stackoverflow.com/a/21252866/456814) was largely identical to the linked blog post. There's actually a lot of plagiarism, intentional or otherwise, resulting from that blog post, not just on this question, but on others around StackOverflow as well. –  Apr 17 '14 at 06:17
  • @Chloe for example, see [this answer](http://stackoverflow.com/a/8825966/456814). –  Apr 17 '14 at 06:23
  • @Cupcake I've tried that, and the original blog post, but it doesn't work for Rails 4. – Chloe Apr 17 '14 at 06:25
  • 1
    @Chloe what are you trying to do? –  Apr 17 '14 at 06:27
  • @Cupcake [This](http://stackoverflow.com/questions/23124933/how-do-i-selectively-enable-ssl-in-rails-for-certain-paths) is what I'm attempting if you'd like to have a look. – Chloe Apr 17 '14 at 06:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50840/discussion-between-cupcake-and-chloe) –  Apr 17 '14 at 06:36

2 Answers2

26

I know you asked for WEBrick, and excuse me for suggesting something else, but I would really recommend you to use Thin ruby web server (faster and lighter), so you can also start SSL and meet your requirement as easy as:

$ thin start --ssl -p 3001

Just don't forget to add gem 'thin' in your Gemfile ;-)

Hugo
  • 2,069
  • 22
  • 23
  • @Chloe I actually use Thin, and I have no problems with it. Lots of people use Thin. Why do you describe it as "artisan"? –  Apr 17 '14 at 06:22
  • Correct me if I'm wrong, but don't you also need to add an SSL certificate if you want to use SSL with Thin? See also [Enable https in a rails app on a thin server](http://stackoverflow.com/q/11589636/456814). –  Apr 17 '14 at 06:27
  • It appears to include its own: `CN: openca.steamheat.net` – Chloe Apr 17 '14 at 06:46
  • 1
    I have to mention that just using `--ssl` is not suitable for production environments, of course. If you want to use this in production, you have to additionally tell Thin to use a valid SSL certificate from a Certificate Authority. –  Apr 17 '14 at 19:23
  • Thin is giving me `Uncaught ReferenceError: jQuery is not defined jquery_ujs.js?body=1:398` errors in the browser console when I reload the page with Rails 4.1. – Chloe Apr 18 '14 at 03:35
  • Thin is great but what lacks is the colorful debug console output as the one displayed with `rails s` – W.M. Mar 11 '17 at 14:01
7

Here's my solution which is compatible with both Rails 3 and 4.

Add the following code at the top of bin/rails:

require 'rubygems'
require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'

module Rails
  class Server < ::Rack::Server
    def default_options
      ssl_default_options = {
        :Port => 443,
        :SSLEnable => true,
        :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
        :SSLPrivateKey => OpenSSL::PKey::RSA.new(File.open(File.expand_path('../../config/cert/server_development.key', __FILE__)).read),
        :SSLCertificate => OpenSSL::X509::Certificate.new(File.open(File.expand_path('../../config/cert/server_development.crt', __FILE__)).read),
        :SSLCertName => [['CN', WEBrick::Utils::getservername]]
      }
      ENV['RAILS_SSL'] ? super.merge(ssl_default_options) : super
    end
  end
end

You obviously need to generate a self signed certificate or buy one.

Then when you want to start WebRick from command line use RAILS_SSL=true rails s. Usually you need sudo to listen on port 443 so you may want to append -p 3001 to change port.

collimarco
  • 34,231
  • 36
  • 108
  • 142