Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
9

Go to list of users who liked

7

Share on X(Twitter)

Share on Facebook

Add to Hatena Bookmark

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Sinatraでローカル環境のWEBrickをHTTPSで動かす

Last updated atPosted at 2017-08-23

TL;DR

  • Sinatra + WEBrick + HTTPS
  • WEBrickは起動時のオプションで下記を指定すればよい
    • SSLEnable: true
    • SSLCertificate: [['CN', WEBrick::Utils.getservername]]1
  • SinatraからWEBrick起動時のオプションを渡すにはset :server_settingsを使う

環境

  • Windows10
    • ruby 2.4.1p111 (2017-03-22 revision 58053) [x64-mingw32]
    • sinatra 1.4.8
  • OSX
    • ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
    • sinatra 2.0.0

なお、Sinatraはクラシックスタイルでの利用

鍵の作成

下記を参考に作成。
Creating a Self-Signed SSL Certificate | Heroku Dev Center
ただ、最終的にWEBrickで自動作成するなら不要。

Sinatraでのサーバへの設定

sinatra/base.rb#L1452
つまり

set:server_settings,options

とすればいいので下記の感じに。

ifsettings.development?require'webrick/https'require'openssl'ssl_options={SSLEnable:true,SSLCertificate:OpenSSL::X509::Certificate.new(File.open('./server.crt').read),SSLPrivateKey:OpenSSL::PKey::RSA.new(File.open('./server.key').read)}set:server_settings,ssl_optionsend
  • server.crtserver.keyは起動前に所定の場所に配置する
    • ただし、WEBrickで自動作成するなら不要
  • if settings.development?は本番では必要ないための分岐

WEBrickで証明書を自動作成するようにする

  1. webrick.rb記載のように[%w[CN localhost]]でもいい。

9

Go to list of users who liked

7
0

Go to list of comments

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9

Go to list of users who liked

7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp