生成證書
這里使用自己生成的免費證書。在${JAVA_HOME}/bin 下可以看到keytool.exe,在改目錄打開cmd然后輸入:
keytool -genkey -v -alias lgy.com -keyalg RSA -keystore d:\lgy.com.keystore -validity 3650
生成證書過程中:【你的名字】對應網站域名或IP。
轉換證書
常用證書格式:JKS(.keystore),微軟(.pfx),OPSSL之PEM(.key + .crt),其中tomcat使用JKS格式,nginx使用PEM格式。
由於生成的證書是jks格式,nginx不能直接用,需要要轉成PEM格式,這要用到jks2pfx工具進行轉換。
jks2pfx的命令格式:JKS2PFX.bat keystore password alias exportname
keystore:KeyStore文件絕對路徑
password:KeyStore文件對應的密碼
alias:生成證書CSR時,所起的Alias別名
exportname:准備導出的文件名稱 (不要帶擴展名)
JKS2PFX.bat d:\lgy.com.keystore 123456 lgy.com exportfile
該命令將server.jks中別名為lgy.com的SSL證書導出,運行后將在jks2pfx的按照目錄產生3個文件:
exportfile.key、exportfile.crt、exportfile.pfx;
配置nginx
- 將exportfile.key、exportfile.crt復制到nginx的conf目錄,並將exportfile.crt重命名未exportfile.pem
- 配置nginx.conf,打開https:
server {
listen 80;
server_name localhost;
#將http請求自動跳轉到https上
return 301 https://$server_name$request_uri;
}
server {
#監聽443端口
listen 443 ssl;
server_name localhost;
#證書路徑。從conf開始找
ssl_certificate exportfile.pem;
ssl_certificate_key exportfile.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
#反向代理http://127.0.0.1:8080
proxy_pass http://127.0.0.1:8080;
}
}