通过jdk自制https证书并配置到nginx并配置http2
生成证书
这里使用自己生成的免费证书。在${JAVA_HOME}/bin 下可以看到keytool.exe,在改目录打开cmd然后输入:
keytool -genkey -v -alias lgq.com -keyalg RSA -keystore d:/zj/ssl/fastfly.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:/ssl/fastfly.com.keystore 123456 fastfly.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 fastfly.com;#将http请求自动跳转到https上return 301 https://$server_name$request_uri;
}server {#监听443端口listen 443 ssl;server_name fastfly.com;#证书路径。从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:8080proxy_pass http://127.0.0.1:8080;}
}