通过jdk自制https证书并配置到nginx中

生成证书

这里使用自己生成的免费证书。在${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;
    }
}
posted @ 2020-08-24 11:42  lee2guang  阅读(1721)  评论(0编辑  收藏  举报