NGINX配置HTTPS時,需要配置HTTPS證書,此時需要進行一些說明,對於Linux來說,通常需要配置兩個文件,證書CRT和祕鑰KEY,但是在Windows中通常是一個文件PFX,本次就拿PFX爲例,說明一下如何將PFX轉換成CRT和KEY並配置到Linux上,從而完成NGINX配置HTTPS。
證書請求
首先需要向權威機構,比如我們使用的是GeoTrust CN RSA CA G1頒發的證書,申請後會下發一個包含公鑰的證書和一個私鑰。
我們此時得到就是****.pfx和一個私鑰KEY。
PFX生成CRT和KEY
區別一下CRT和CER,二者都是證書的意思,CRT常用於Linux,CER常用於Windows,二者都有兩種編碼格式PEM和DER。我們瞭解到,PEM常用於Linux,同時Linux還需要一個祕鑰文件KEY;DER常用於Windows,因此我們需要通過PFX生成一個PEM格式編碼的CRT文件,和一個KEY祕鑰文件配置到Linux中。
生成祕鑰
openssl pkcs12 -in test.com.pfx -out server.key -nocerts
此時需要輸入我們自己私鑰三次。
生成證書
openssl pkcs12 -in test.com.pfx -out server.crt
同樣需要輸入三次我們的祕鑰。
生成PEM
openssl x509 -in server.crt -outform PEM -out server.pem
最後查看生成的文件
配置NGINX
將KEY,PEM移動到指定位置/usr/local/nginx/conf/key
然後在nginx中配置一下內容。
server {
listen 443 ssl;
server_name 域名;
ssl_certificate /usr/local/nginx/conf/key/server.pem;
ssl_certificate_key /usr/local/nginx/conf/key/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
error_page 500 502 503 504 /50x.html;
error_page 404 /404.html;
error_page 403 /403.html;
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location = /403.html {
root html;
}
location / {
.............
.............
}
}