摘要:介绍如何生成自签名证书,和在ubuntu下使用Caddy代理,使http网站变为https
一、生成自签名证书
这篇文章说的很详细了https://gist.github.com/cecilemuller/9492b848eb8fe46d462abeb26656c4f8。
1、安装openssl,
windows下,http://slproweb.com/products/Win32OpenSSL.html
window下选最大的
ubuntu下,https://www.openssl.org/source/
说明:即使你的命令行输入openssl有反应,也建议重装,因为很有可能是git或apache的openssl,并不能用于签名
2、生成CA证书
openssl req -x509 -nodes -new -sha256 -days 1024 -newkey rsa:2048 -keyout RootCA.key -out RootCA.pem -subj "/C=US/CN=Example-Root-CA"
openssl x509 -outform pem -in RootCA.pem -out RootCA.crt
Example随便改吧
3、新建domains.ext文件
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = localhost
IP.1 = 127.0.0.1
IP.2 = 192.168.1.60
IP.3 = 192.168.1.61
alt_names是你的网站要通过https方式访问的域名或IP地址
4、生成证书
openssl req -new -nodes -newkey rsa:2048 -keyout localhost.key -out localhost.csr -subj "/C=US/ST=YourState/L=YourCity/O=Example-Certificates/CN=localhost.local"
openssl x509 -req -sha256 -days 1024 -in localhost.csr -CA RootCA.pem -CAkey RootCA.key -CAcreateserial -extfile domains.ext -out localhost.crt
5、其中key和crt文件一般是网站服务器要使用的证书
6、使浏览器信任CA证书:谷歌浏览器->设置->安全性和隐私->一般是tab3,有一个证书列表,添加证书。过程中弹出的对话框都勾选信任即可。windows用的是RootCA.crt,ubuntu用的是RootCA.pem
7、为了安全,切勿让他人得到你的证书
二、Caddy开启HTTPS代理
1、下载Caddy,https://github.com/caddyserver/caddy/releases,找到最新版的releases的asset,windows exe后缀即可,ubuntu 选包含linux的即可
2、caddy是go编写的,就是一整个文件,ubuntu下添加执行权限。
3、把caddy和签发的crt,key文件放一块,再新建Caddyfile如下
https://192.168.1.61:5090
tls ./localhost.crt ./localhost.key
reverse_proxy http://192.168.1.61:9280
第一个是代理端口,第二个是被代理端口
然后启动:
sudo ./caddy run --config Caddyfile
就OK了,通过代理端口访问即是https访问