How to make Sinatra work over HTTPS/SSL?

后端 未结 5 1630
闹比i
闹比i 2020-11-30 21:40

As the title says, Google doesn\'t give anything useful concerning this.

How do I set up and configure HTTPS/SSL for Sinatra apps?

How do I create a HTTPS ro

5条回答
  •  孤城傲影
    2020-11-30 21:57

    I modified code of richard_bw as to be able close or restart it with Ctrl+C:

    require 'sinatra/base'
    require 'webrick'
    require 'webrick/https'
    require 'openssl'
    
    class MyServer  < Sinatra::Base
        post '/' do
          "Hello, world!\n"
        end            
    end
    
    CERT_PATH = '/opt/myCA/server/'
    
    webrick_options = {
      :Port               => 8443,
      :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
      :DocumentRoot       => "/ruby/htdocs",
      :SSLEnable          => true,
      :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
      :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "server.crt")).read),
      :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "server.key")).read),
      :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ],
      :app                => MyServer
    }
    Rack::Server.start webrick_options
    

提交回复
热议问题