399 字
2 分钟
使用mkcert进行本地自签证书
在本地开发时,常常会有非https的“不安全”的提示,去获得权威CA的证书又不现实。但借助mkcert,可以方便的自签证书。
首先,上地址:
A simple zero-config tool to make locally trusted development certificates with any names you'd like.
安装
按照介绍进行安装,我在Windows下使用scoop进行安装:
scoop bucket add extrasscoop install mkcert其它系统或包管理器可参照介绍页的提示。
信任CA证书
本地
mkcert可以一键将CA证书装在本机上并信任。
$ mkcert -installCreated a new local CA 💥The local CA is now installed in the system trust store! ⚡️The local CA is now installed in the Firefox trust store (requires browser restart)! 🦊他人
要让他人信任你的CA,可以把公钥发给他人,查看路径:
$ mkcert -CAROOT一般是在~\AppData\Local\mkcert里,把rootCA.pem发给别人即可(千万不要把key私钥发出去了哦!!)
生成证书
在一个文件夹内,输入mkcert your.website.com「自行替换成你需要的地址,如localhost」,然后即可在你的文件夹里得到证书your.website.com.pem和密钥your.website.com-key.pem。
搭建反代服务器
以nginx为例,下载好nginx,把两个文件拖入./conf中,然后配置nginx.conf,以下是最简单的反代配置:
server { listen 1145 ssl; #端口 server_name localhost;
ssl_certificate your.website.com.pem; #你的证书文件名 ssl_certificate_key your.website.com-key.pem; #你的密钥文件名
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
location / { proxy_pass http://127.0.0.1:8080; #要反代的地址 } }可以配置多个server,分别监听前端和后端。「可选」
如果没有问题的话,加上https就可以看到锁了。 图中效果是配合修改系统hosts达成的:

