Skip to content

Instantly share code, notes, and snippets.

@k-ta-yamada
Last active January 14, 2021 00:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k-ta-yamada/6d3d6c75980dee5cf1dd189e170badce to your computer and use it in GitHub Desktop.
Save k-ta-yamada/6d3d6c75980dee5cf1dd189e170badce to your computer and use it in GitHub Desktop.
Sinatra + WEBrick + HTTPS
require 'sinatra'
require 'webrick/https'
set :server_settings,
SSLEnable: true,
SSLCertName: [['CN', WEBrick::Utils.getservername]]
get '/' do
'https page'
end
@k-ta-yamada
Copy link
Author

https://github.com/ruby/ruby/blob/trunk/lib/webrick/ssl.rb#L20-L22

    # WEBrick can automatically create a self-signed certificate if
    # <code>:SSLCertName</code> is set.  For more information on the various
    # SSL options see OpenSSL::SSL::SSLContext.

@k-ta-yamada
Copy link
Author

https://github.com/ruby/ruby/blob/202bbda2bf5f25343e286099140fb9282880ecba/lib/webrick.rb#L88-L121

# == HTTPS
#
# To create an HTTPS server you only need to enable SSL and provide an SSL
# certificate name:
#
#   require 'webrick'
#   require 'webrick/https'
#
#   cert_name = [
#     %w[CN localhost],
#   ]
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertName => cert_name)
#
# This will start the server with a self-generated self-signed certificate.
# The certificate will be changed every time the server is restarted.
#
# To create a server with a pre-determined key and certificate you can provide
# them:
#
#   require 'webrick'
#   require 'webrick/https'
#   require 'openssl'
#
#   cert = OpenSSL::X509::Certificate.new File.read '/path/to/cert.pem'
#   pkey = OpenSSL::PKey::RSA.new File.read '/path/to/pkey.pem'
#
#   server = WEBrick::HTTPServer.new(:Port => 8000,
#                                    :SSLEnable => true,
#                                    :SSLCertificate => cert,
#                                    :SSLPrivateKey => pkey)
#

@k-ta-yamada
Copy link
Author

Sinatra(WEBrick)でsslサーバをたてる
https://gist.github.com/suruseas/3489236

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment