前言
在项目中有时会用到自签名证书,因为在内网使用,如果用域名比较麻烦,还需要搭建DNS服务器,所以直接使用IP做https签名证书
实现
最原始的做法肯定是用openssl等一系列操作,但是太麻烦,最近在github上看到了一个工具,非常的方便,下载地址,mkcert,非常简单好用,下面介绍下
mkcert基本使用
从以上连接下载最新的对应的系统版本,windows的下载后最好放到system32下,这样使用起来比较方便,之后直接在cmd里输入mkcert,如下,会输出使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| C:\Users> mkcert Using the local CA at "C:\Users\admin\AppData\Local\mkcert" ✨ Usage of mkcert:
$ mkcert -install Install the local CA in the system trust store.
$ mkcert example.org Generate "example.org.pem" and "example.org-key.pem".
$ mkcert example.com myapp.dev localhost 127.0.0.1 ::1 Generate "example.com+4.pem" and "example.com+4-key.pem".
$ mkcert "*.example.it" Generate "_wildcard.example.it.pem" and "_wildcard.example.it-key.pem".
$ mkcert -uninstall Uninstall the local CA (but do not delete it).
For more options, run "mkcert -help".
|
首先我们要执行下 mkcert -install,这是安装一下根证书,避免在浏览器出现不安全连接等问题
生成自签名证书
比如我们要为指定IP生成一个https证书,则如下代码
1 2 3 4 5 6
| mkcert -cert-file D:/server.crt -key-file D:/server.key 192.168.100.239 t.cn localhost 127.0.0.1 ::1
|
nginx使用https证书配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| server { listen 80; server_name 192.168.31.39 my.dev localhost 127.0.0.1 ::1; rewrite ^(.*) https://$host$1 permanent; }
server { listen 443 ssl; server_name 192.168.31.39 my.dev localhost 127.0.0.1 ::1;
ssl_certificate D:/android/down/nginx-1.20.2/ssl/server.crt; ssl_certificate_key D:/android/down/nginx-1.20.2/ssl/server.key;
ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on;
location ~* ^.+\.(ico|gif|jpg|jpeg|png|html|htm|js|css|svg|woff|ttf)$ { expires 3d; root html; }
location / { proxy_pass http://127.0.0.1:9999; } }
|