Configuring WEBrick to use SSL in Rails 4

北城以北 提交于 2019-11-27 00:56:58

问题


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.


回答1:


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 ;-)




回答2:


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.



来源:https://stackoverflow.com/questions/21189920/configuring-webrick-to-use-ssl-in-rails-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!