https をローカル(LAN)で使いたい

httpsサーバーは、基本的に Let’s Encrypt を使用すればよい
が、テスト等でLANでhttpsを使用したい場合、困ることがある
この場合、自家製の証明書を作成して使用する事で回避できる

尚、公開とローカル同時使用のサーバーの場合
nginxにserver{}を複数作成することでポート毎に複数の証明書
を使用できる

証明書に記載するURLは、IPアドレスではダメな模様
端末がwindowsの場合 /windows/system32/drivers/etc/hosts
に登録しておくと良い

opensslで証明書を作成する際、以下のrsa.cnfファイルを作成
しておいてから、.cnf指定で実行するのがお勧め

以下 rsa.cnf のサンプル


[ req ]
prompt = no
days = 3650
distinguished_name = req_distinguished_name
req_extensions = v3_req
x509_extensions = v3_ca

[ req_distinguished_name ]
countryName = JP
stateOrProvinceName = Saitama
localityName = Tokorozawa
organizationName = Miteel Co.,LTD
organizationalUnitName = HQ
commonName = Miteel Local Certificate
emailAddress = xxxxxx@xxxx.xxx.xx

[ v3_req ]
basicConstraints = CA:false
keyUsage = digitalSignature, nonRepudiation, keyEncipherment
subjectAltName = @alt_names

[ v3_ca ]
subjectAltName = @alt_names
keyUsage = digitalSignature, keyEncipherment
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
issuerAltName = issuer:copy

[ x509 ]
x509_extensions = x509v3_config

[ x509v3_config ]
copy_extensions = copy

[ req_ext ]
subjectAltName = @alt_names

[alt_names]
DNS.0 = xxxxxx1.com
DNS.1 = xxx2xxx.com
DNS.2 = x3xxxxx.com
email.1 = xxxx@xxx.xxx.xx

以下を実行すると key.pem と server.crt が得られる

openssl genrsa -out key.pem 3072
openssl req -new -x509 -key key.pem -sha256 -out server.crt -days 3600 -config rsa.cnf

nginx のconfigは以下を参考に設定を!

server {
listen 80;
listen [::]:80;
listen 443 ssl;
ssl_certificate "/xxxxxx/server.crt";
ssl_certificate_key "/xxxxxx/key.pem";
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
server_name test.com;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}

端末側では、証明書を certmgr.exe 信頼されたルート証明書
にインポートしておく必要がある
また、インポート後は、再起動しないとダメ

また証明書は、それをインポートする前に端末でhttps接続する
と ”x保護されていない通信” 状態となるので、
これを右クリックして 証明書の詳細をクリックすると
証明書ビューア が出るので 詳細タブをクリック
エクスポートボタンで保存できる
これを certmgr.exe で信頼されたルート証明書にインポート
したあと、再起動すればよい