今天設定客戶的域名遇到 SSL_CTX_use_PrivateKey_fileEVP_DecryptFinal_ex:bad decrypt error 的錯誤訊息 指出是私鑰的問題。

May 07 16:43:18 terrylin nginx[5307]: nginx: [emerg] SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/terryl.in/2020/server.key") failed (SSL: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt error:0May 07 16:43:18 terrylin nginx[5307]: nginx: configuration file /etc/nginx/nginx.conf test failed
May 07 16:43:18 terrylin systemd[1]: nginx.service: Control process exited, code=exited status=1
May 07 16:43:18 terrylin systemd[1]: nginx.service: Failed with result 'exit-code'.
May 07 16:43:18 terrylin systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed

打開私鑰檔案,發現在以往的長的不一樣,開頭多了一些編碼資訊。

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,D5F9600F17CDAB82

這是被密碼保護的私鑰。所以解決方法有兩種方式,一是補上設定在 nginx 的設定檔中。舉例,我們建立一個文字檔,命名為 ssl_password.txt 並將密碼存放在這個檔案。

在 Nginx 的設定中補上這一行。

ssl_password_file /etc/nginx/ssl/terryl.in/2020/ssl_password.txt;

重啟 Nginx,就可以了。

另外一種方法則是把有密碼保護的私鑰檔案轉成不需要密碼的私鑰。利用 OpenSSL 的指令如下:

openssl rsa -in server.key -out new_server.key

會提示需要您輸入密碼。

Enter pass phrase for server.key:

密碼正確輸入的話,則會匯出 new_server.key,這是不需要密碼保護的私鑰檔案。當然 Nginx 的設定就照之前的設定就可以了。

最後修改日期: 2021-02-11

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。