LoginSignup
3
4

More than 3 years have passed since last update.

railsのlocal環境をhttpsにしてみた。

Last updated at Posted at 2020-07-28

環境

ruby 2.5.1
rails 6.0.3.2
mac catalina 10.15.5

mkcert install

Git mkcert

$ brew install mkcert

↑私の場合はprojectsフォルダで実行
↓localにCA(認証局)を作成

$ mkcert -install

証明書とキー発行

作成中のapplicationのフォルダへ移動
該当アプリの.gitignoreに下記追加。※Gitに挙げないため。

localhost.pem
localhost-key.pem

以下を実行

$ mkcert localhost

Sudo password:を求められます。
これは自身のmacのpasswordです。

これで、
localhost.pem
localhost-key.pem
の2つのファイルが出来上がります。

config/puma.rbに追記

config/puma.rb
if ENV.fetch('RAILS_ENV') { 'development' } == 'development'
  ssl_bind '0.0.0.0', '9292', key: './localhost-key.pem', cert: './localhost.pem'
end

以上で準備終了

rails起動して以下をポチっ
https://localhost:9292/

追記

アンインスト

ターミナル/私の場合はprojectsフォルダ
mkcert -uninstall

config/puma.rbから上記を削除

ブラウザの設定を変更する

ブラウザは一度httpsで接続したサイトはhttpsで接続しようとします(仮にアドレスバーにhttp://localhost:3000/と直接入力しても)。
その設定が簡単に変わらないようです。
安易に変更するともとに戻せなくなります。私のように。
お気をつけください。

3
4
0

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
3
4