内网使用mkcert签名证书

简介

mkcert 是一个用于生成本地自签名 SSL 证书的开源工具,项目基于 Golang 开发,可跨平台使用,不需要配置,支持多域名以及自动信任 CA。

请注意,自签名的证书只在开发和测试环境中使用,并不适用于生产环境。在生产环境中,你应该使用由受信任的证书颁发机构(CA)签名的证书。

准备工作

局域网内访问 Web 服务的场景,因此开始前需要:

  • 服务器:用于提供 Web 服务,安装 nginx 并开启 SSL,系统为 CentOS 7.8。
  • PC 端:一台 Win10 电脑,用于测试访问,与服务器处于同一局域网。

下载安装

wget -O mkcert https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64

赋权验证

cp mkcert-v1.4.4-linux-amd64 /usr/local/bin/mkcert
chmod +x /usr/local/bin/mkcert
mkcert -version

生成证书文件

[root@centos-77 ~]# mkcert 192.168.3.77 127.0.0.1 ::1 localhost
Created a new local CA 💥
Note: the local CA is not installed in the system trust store.
Note: the local CA is not installed in the Firefox and/or Chrome/Chromium trust store.
Run "mkcert -install" for certificates to be trusted automatically ⚠️
Created a new certificate valid for the following names 📜
- "192.168.3.77"
- "127.0.0.1"
- "::1"
- "localhost"
The certificate is at "./192.168.3.77+3.pem" and the key at "./192.168.3.77+3-key.pem"
It will expire on 14 October 2025 🗓

由于服务器上已经装了nginx,命令中可以加入 -cert-file 和 -key-file 参数,将文件直接生成到对应的目录里:

生成证书文件到nginx目录

[root@centos-77 ~]# mkcert -cert-file /etc/nginx/certs/server.crt -key-file /etc/nginx/certs/server.key 192.168.3.77 127.0.0.1 ::1 localhost
Note: the local CA is not installed in the system trust store.
Note: the local CA is not installed in the Firefox and/or Chrome/Chromium trust store.
Run "mkcert -install" for certificates to be trusted automatically ⚠️
Created a new certificate valid for the following names 📜
- "192.168.3.77"
- "127.0.0.1"
- "::1"
- "localhost"
The certificate is at "/etc/nginx/certs/server.crt" and the key at "/etc/nginx/certs/server.key"
It will expire on 14 October 2025 🗓

配置nginx

mkdir -p /etc/nginx/certs
server {
listen 80;
listen [::]:80;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name _;
ssl_certificate /etc/nginx/certs/server.crt;
ssl_certificate_key /etc/nginx/certs/server.key;
nginx -t
nginx -s reload

mkcert的认证机构安装到服务器上

[root@centos-77 ~]# mkcert -install
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox and/or Chrome/Chromium trust store (requires browser restart)! 🦊

查看CA证书的位置

[root@centos-77 ~]# mkcert -CAROOT
/root/.local/share/mkcert

该目录中有两个文件:rootCA-key.pemrootCA.pem。将 rootCA.pem 复制到 PC 上,并将其后缀改为 .crt

双击 rootCA.crt,根据提示安装证书,步骤如下:
image
image
image
image

浏览器访问验证,看到连接已经变为安全:

image

本文作者:眈眈逐逐

本文链接:https://www.cnblogs.com/xjzyy/p/17553820.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   眈眈逐逐  阅读(1566)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
-->
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 时光,一如既往 房东的猫
  2. 2 你的耳机 愚青
  3. 3 是初恋是心动是你 房东的猫
  4. 4 像鱼 王贰浪
  5. 5 今夜的江滩公园没有烟花跨年 房东的猫
  6. 6 我可以 房东的猫
  7. 7 孤独她呀 二珂
  8. 8 New Boy 房东的猫 / 陈婧霏
  9. 9 小镇姑娘 你的九儿
  10. 10 秋酿 房东的猫
  11. 11 爱要怎么说出口 澪恩Seiwen
时光,一如既往 - 房东的猫
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.