To enable HTTPS, require webrick/https and set :SSLEnable => true.
Option 1: Self-signed certificate
Provide a :SSLCertName array (e.g., [['CN', 'localhost']]). WEBrick will generate a self-signed certificate that changes every time the server restarts.
Option 2: Pre-determined certificate and key
Provide an OpenSSL::X509::Certificate object to :SSLCertificate and an OpenSSL::PKey::RSA object to :SSLPrivateKey.
require 'webrick'
require 'webrick/https'
# Using self-signed certificate
server = WEBrick::HTTPServer.new(
:Port => 8000,
:SSLEnable => true,
:SSLCertName => [['CN', 'localhost']]
)
# Using existing certificate and key
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
)