HTTPS localhost 在 localhost mkcert 上使用 HTTPS 的真正简单方法

2025-05-26

HTTPS localhost在本地主机上使用 HTTPS 的真正简单方法

mkcert

测试您的网站在本地机器上是否运行良好总是很麻烦的。

我发现了一个可以使它变得非常简单的工具,mkcert



➜  localhost-https mkcert -install
Using the local CA at "/Users/.../mkcert"
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)! 🦊
The local CA is now installed in Java''s trust store! ☕️

➜  localhost-https mkcert localhost
Using the local CA at "/Users/.../mkcert"

Created a new certificate valid for the following names 📜
 - "localhost"

The certificate is at "./localhost.pem" and the key at "./localhost-key.pem"


Enter fullscreen mode Exit fullscreen mode

然后你可以用一个简单的 HTML 页面进行测试:



➜  localhost-https cat index.html
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: index.html
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ <html>
   2   │ <body>
   3   │ HELLO WORLD
   4   │ </body>
   5   │ </html>
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
➜  localhost-https ./node_modules/http-server/bin/http-server -S -C ./localhost.pem -K ./localhost-key.pem                                                                                                                         [18:12:41]
Starting up http-server, serving ./ through https
Available on:
  https://127.0.0.1:8080
  https://192.168.1.69:8080
Hit CTRL-C to stop the server


Enter fullscreen mode Exit fullscreen mode

结果如下:

GitHub 徽标 FiloSottile / mkcert

一个简单的零配置工具,可以以任何您想要的名称制作本地受信任的开发证书。

mkcert

mkcert 是一个用于创建本地信任的开发证书的简单工具,无需任何配置。

$ mkcert -install
Created 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)! 🦊

$ mkcert example.com "*.example.com" example.test localhost 127.0.0.1 ::1

Created a new certificate valid for the following names 📜
 - "example.com"
 - "*.example.com"
 - "example.test"
 - "localhost"
 - "127.0.0.1"
 - "::1"

The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅

Chrome 和 Firefox 屏幕截图

使用来自真实证书颁发机构 (CA) 的证书进行开发可能很危险,甚至无法实现(例如example.testlocalhost或 等主机127.0.0.1),但自签名证书会导致信任错误。管理自己的 CA 是最佳解决方案,但通常需要复杂的命令、专业知识和手动步骤。

mkcert 自动在系统中创建并安装本地 CA...

文章来源:https://dev.to/rhymes/really-easy-way-to-use-https-on-localhost-341m
PREV
2018 年 dev.to 上最受欢迎的 10 篇文章
NEXT
如何用 3 行代码实现 Python 代码并发