概要

https接続するために自己証明書(オレオレ証明書)を作成するのはまぁ手間がかかります。
しかし、localhostというGemを使えばSSL証明書を自作せずにRailsの開発環境でhttps接続できます。
今回のコードはこちら

  • Ruby: 3.0.2
  • Ruby on Rails: 7.0.0
  • Puma: 5.5.2
  • localhost: 1.1.9

localhost Gemとは

localhost Gemとは、Gemを追加するだけhttps://localhost:8080のようにSSL接続できる便利なライブラリです。
自動的に秘密鍵及び、自己証明書(オレオレ証明書)を作成してくれます。

設定方法

開発環境だけlocalhost Gemを使いたいので、GemfileのdevelopmentグループブロックにGemを追加。

# Gemfile
group :development do
  gem 'localhost'
end

実行

Pumaコマンドであれば、Gem追加だけでhttps接続ができます。

$ bundle exec puma -b 'ssl://localhost:9292' config.ru

Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 14848
* Listening on ssl://127.0.0.1:9292?
* Listening on ssl://[::1]:9292?
Use Ctrl-C to stop

ただ、これだとアプリケーションログはlog/development.logに出力されるので、別タブを開いてtailコマンド等で参照するしかありません。 rails serverで実行できた方が何かと便利です。

Rails serverで動かす

Pumaの設定ファイルにSSL接続の設定を追加します。
Railsデフォルトの設定で3000番ポートは使われているので3001番ポートを指定します。

# config/puma.rb
require 'localhost/authority'
authority = Localhost::Authority.fetch

ssl_bind '127.0.0.1', '3001', {
  key: authority.key_path,
  cert: authority.certificate_path
}

Railsコマンドで実行する。

$ bundle exec rails server

=> Booting Puma
=> Rails 7.0.0 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 17087
* Listening on ssl://127.0.0.1:3001?cert=/Users/kseki/.localhost/localhost.crt&key=/Users/kseki/.localhost/localhost.key&verify_mode=none
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000
Use Ctrl-C to stop

リスニングしているポートが2つあります。
https://localhost:3001にアクセスすると続けてアプリケーションログも出力されます。

Started GET "/" for 127.0.0.1 at 2021-12-31 23:20:45 +0900
Processing by HomeController#index as HTML
  Rendering layout layouts/application.html.erb
  Rendering home/index.html.erb within layouts/application
  Rendered home/index.html.erb within layouts/application (Duration: 0.0ms | Allocations: 15)
  Rendered layout layouts/application.html.erb (Duration: 0.6ms | Allocations: 484)
Completed 200 OK in 1ms (Views: 1.0ms | Allocations: 733)

当然http://localhost:3000にアクセスしても動きます。

ポート3000番で接続したい

更にhttpsだけで接続するようにするにはPumaの設定を変更します。

  • ssl_bindメソッドの引数を3001から3000に変更
  • PORT指定行をコメントアウト
# config/puma.rb

- ssl_bind '127.0.0.1', '3001', {
+ ssl_bind '127.0.0.1', '3000', {

- port ENV.fetch("PORT") { 3000 }
+ # port ENV.fetch("PORT") { 3000 }
$ bundle exec rails server

=> Booting Puma
=> Rails 7.0.0 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 3.0.2-p107) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 20097
* Listening on ssl://127.0.0.1:3000?cert=/Users/kseki/.localhost/localhost.crt&key=/Users/kseki/.localhost/localhost.key&verify_mode=none
Use Ctrl-C to stop

https://localhost:3000にアクセスすると続けてアプリケーションログが出力されます。

Started GET "/" for 127.0.0.1 at 2021-12-31 23:28:51 +0900
Processing by HomeController#index as HTML
  Rendering layout layouts/application.html.erb
  Rendering home/index.html.erb within layouts/application
  Rendered home/index.html.erb within layouts/application (Duration: 1.4ms | Allocations: 448)
  Rendered layout layouts/application.html.erb (Duration: 4.6ms | Allocations: 1473)
Completed 200 OK in 12ms (Views: 8.2ms | Allocations: 4696)

まとめ

Gistに必要箇所だけまとめたので御覧ください。

参考