はじめに
ローカル認証局(CA)の機能を提供するmkcertを使用して、MacOSのローカル環境でHTTPSとカスタムドメインを導入してみましょう!
mkcertの設置
brewを利用してパソコンにmkcertを設置します。
brew install mkcert
認証局(CA)の設置
mkcertのコマンドを利用してMacOSにローカル認証局(CA)のルート証明書を設置します。
mkcert -install
SSL証明書
config/ssl
ディレクトリにSSL証明書とプライベートキーを生成します。
# Railsプロジェクトのルートディレクトリで実行
mkdir -p config/ssl && cd $_ && mkcert "*.custom_domain.localhost"
Rails環境構成の変更
config/environments/development.rb
ファイルにカスタムドメインを追加します。
# config/environments/development.rb
config.force_ssl = true
config.hosts << '.custom_domain.localhost'
Railsアプリの実行
ローカルでSSL証明書とプライベートキーを利用してRailsアプリを実行します。
rails server -b 'ssl://0.0.0.0:3443?key=config/ssl/_wildcard.custom_domain.localhost-key.pem&cert=config/ssl/_wildcard.custom_domain.localhost.pem'
参考文献