開発環境を https 化する mkcert を試してみる。

こんにちは k-jun です。今回は手軽に SSL 証明書を発行できる cli ツール mkcert を試してみます。

https://github.com/FiloSottile/mkcert

開発環境の https 化を目指したものなので、オレオレ証明書の自動作成ツールとはなりませんが。

Install

$ brew install mkcert

Run

$ mkcert filippo@example.com
Note: the local CA is not installed in the system trust store.
Run "mkcert -install" for certificates to be trusted automatically ⚠️

Created a new certificate valid for the following names 📜
 - "filippo@example.com"

The certificate is at "./filippo@example.com.pem" and the key at "./filippo@example.com-key.pem" ✅

It will expire on 2 January 2024 🗓
$ ls filippo@example.com*
filippo@example.com-key.pem     filippo@example.com.pem

S/MIME certificate なんてものもあるんですね。初めて知りました...。 適当に証明書を作ってみます。ドメインlocalhostで。

$ mkcert localhost
Note: the local CA is not installed in the system trust store.
Run "mkcert -install" for certificates to be trusted automatically ⚠️

Created a new certificate valid for the following names 📜
 - "localhost"

The certificate is at "./localhost.pem" and the key at "./localhost-key.pem" ✅

It will expire on 4 January 2024 🗓

localhost.pemlocalhost-key.pem が生成されました。これを用いて SSL の設定をしていきます。Docker で Nginx を起動し、これに設定ができれば大丈夫と思いますのでこれでやっていきます。

# nginx.conf
http {
  server_names_hash_bucket_size 128; # this seems to be required for some vhosts

  server { # simple reverse-proxy

    listen 443;
    ssl on;
    ssl_certificate /tmp/localhost.pem;
    ssl_certificate_key /tmp/localhost-key.pem;

    location / {
      # proxy_pass http://host.docker.internal:5000;
      return 200 "Hello World";
    }
  }
}[f:id:K-jun1221:20211004012754p:plain]
# docker-compose.yml
version: "3.3"
services:
  nginx: 
    ports:
      # - "8080:80"
      - "8443:443"
    image: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - /tmp:/tmp

大丈夫そう。それでは起動します。

$ docker-compose up
Starting nginx_nginx_1 ... done
Attaching to nginx_nginx_1
nginx_1  | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1  | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1  | 10-listen-on-ipv6-by-default.sh: error: IPv6 listen already enabled
nginx_1  | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1  | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx_1  | 2021/10/03 16:28:30 [warn] 1#1: the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/nginx.conf:30
nginx_1  | nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/nginx.conf:30

Chrome からアクセスしてみる。

[f:id:K-jun1221:20211004012754p:plain

大丈夫そうですね! 開発環境の https は mkcert で簡単に実現できそうです。正直最初はオレオレ証明書簡単生成ツールとして見ていましたが、開発環境限定でした...。 まあ、オレオレ証明書ぐらい自分で生成しろって話ですね。それでは今回はこのへんで。