LoginSignup
52
47

More than 3 years have passed since last update.

Nginx で SSL証明書 の設定時のトラブルシューティング方法

Last updated at Posted at 2015-08-21

背景

  • Nginx で HTTPS 通信するため、証明書関連の設定で時間を要した
  • まとまったドキュメントが見つからなかったので、ノウハウをまとめてみた。

設定項目

Nginx の SSL 証明書関連で大切なのは次の設定。

        ssl on;
        ssl_certificate /etc/ssl/certs/www.example.com.chained.cert;
        ssl_certificate_key /etc/ssl/certs/www.example.com.key;

Nginx から、指定したファイルがアクセス可能かどうか、権限を確認する。
また、ファイル名が誤っていないか、確認する。

作成した証明書の確認

特に下記のコマンドは有用

秘密鍵の確認

$ openssl rsa -in /etc/ssl/certs/www.example.com.key -check
RSA key ok
writing RSA key
-----BEGIN RSA PRIVATE KEY-----
MIIEp ...
中略
-----END RSA PRIVATE KEY-----

公開鍵の確認

$ openssl x509 -in /etc/ssl/certs/www.example.com.cert -text -noout
Certificate:
    Data:
        Version
(略)

中間証明書の確認

GoDaddy の証明書を使っているので、サイトから中間証明書を事前にダウンロードしておく。
https://certs.godaddy.com/repository/

ダウンロードした中間証明書が正しいか確認する。

$ openssl x509 -in gd_bundle-g2-g1.crt  -text -noout
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number: 7 (0x7)
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=US, ST=Arizona, L=Scottsdale, O=GoDaddy.com, Inc., CN=Go Daddy Root Certificate Authority - G2
        Validity
            Not Before: May  3 07:00:00 2011 GMT
            Not After : May  3 07:00:00 2031 GMT
        Subject: C=US, ST=Arizona, L=Scottsdale, O=GoDaddy.com, Inc., OU=http://certs.godaddy.com/repository/, CN=Go Daddy Secure Certificate Authority - G2
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (2048 bit)
                Modulus:
(略)

秘密鍵、公開鍵が対応しているかの確認

秘密鍵の modulus を表示

$ openssl rsa  -in /etc/ssl/certs/www.example.com.key  -modulus -noout
Modulus=D3DF880A(snip)

公開鍵の modulus を表示

$ openssl x509  -in /etc/ssl/certs/www.example.com.cert -noout -modulus
Modulus=D3DF880A(snip)

秘密鍵と公開鍵の modulus が同じことを確認する。

公開鍵と中間証明書が対応しているかの確認

$ openssl x509 -issuer_hash -noout -in /etc/ssl/certs/www.example.com.cert
85cf5865
$ openssl x509 -subject_hash -noout -in gd_bundle-g2-g1.crt  -text -noout
85cf5865

ハッシュ値が同じことを確認する。

鍵の生成

Nginx では中間証明書を含めて、1つのファイルにする必要がある。

$ cat www.example.com.cert gd_bundle-g2-g1.crt > www.example.com.chained.cert

Nginx が SSL 有効かを確認

$ nginx -V

下記の表示を確認する。

TLS SNI support enabled
(前略)--with-http_ssl_module (略)

SSL 通信の確認

Nginx の設定が正しいどうか、openssl コマンドで接続して、確認できる。

$ openssl s_client -connect www.example.com:443 -showcerts
(略)
    Verify return code: 21 (unable to verify the first certificate)

などと表示されれば、正しく設定できていない。
中間証明書などを確認する。

$ openssl s_client -connect www.example.com:443 -showcerts
(略)
  Verify return code: 0 (ok)

と表示されれば、成功である。

私の場合は、gd_bundle-g2-g1.crt だとうまくいったのだが、
それではないものをベースに最初試していたため、ハマってしまっていた。

CloudFront 経由(SNI)で正しく動作するかの確認

CloudFront を経由するときなど、SNI を利用するときは、 -servername オプション付きで実行する。

$ openssl s_client -connect xxxxxxxxxxx.cloudfront.net:443 -servername www.example.com -showcerts
(略)
    Verify return code: 0 (ok)

最後に Verify return code: 0 (ok) が表示されれば成功である。

52
47
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
52
47