Golang Web 自动安装、更新Let’s Encrypt 免费证书
🕔 Thu, 30 Nov 2017 by GolangNote
ssl 对网站越来越重要,用户信息安全,搜索引擎信赖... Let’s Encrypt 免费证书最近发展惊人。
如果使用nginx ,则需要配置、签发证书、添加定时任务更新,涉及的东西太多。如果使用go 则轻松在一个可实行包里完成所有功能。下面是使用官方包golang.org/x/crypto/acme/autocert
做个简单示例。
首先配置autocert.Manager:
autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domain), //your domain here
Cache: autocert.DirCache("certs"), //folder for storing certificates
Email: contactEmail,
}
HostPolicy
是要注册的域名,可填写多个域名:autocert.HostWhitelist(domain, domain2, domain3)
,如果留空则是任何解析向该服务器ip 的域名。Cache
是存放证书的目录- 还有个参数
RenewBefore
是指定更新证书的时间,如果不填则是在过期前30天自动更新。
再配置tls.Config:
tls.Config{
GetCertificate: certManager.GetCertificate,
NextProtos: []string{http2.NextProtoTLS, "http/1.1"},
MinVersion: tls.VersionTLS12,
}
还要添加一个协程来监听80 端口并转向443 端口
go func(domain string) {
http.ListenAndServe(":http", http.RedirectHandler("https://"+domain, 301))
}(domain)
全部代码:
package main
import (
"crypto/tls"
"golang.org/x/crypto/acme/autocert"
"golang.org/x/net/http2"
"net/http"
)
const (
contactEmail = "youremail@example.com"
domain = "www.golangnote.com"
)
func main() {
//go func(domain string) {
// http.ListenAndServe(":http", http.RedirectHandler("https://"+domain, 301))
//}(domain)
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domain), //your domain here
Cache: autocert.DirCache("certs"), //folder for storing certificates
Email: contactEmail,
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world ssl!"))
})
go http.ListenAndServe(":http", certManager.HTTPHandler(nil)) // 支持 http-01
server := &http.Server{
Addr: ":https",
TLSConfig: &tls.Config{
GetCertificate: certManager.GetCertificate,
NextProtos: []string{http2.NextProtoTLS, "http/1.1"},
MinVersion: tls.VersionTLS12,
},
MaxHeaderBytes: 32 << 20,
}
server.ListenAndServeTLS("", "") //key and cert are comming from Let's Encrypt
}
这样的好处是方便,弊端是80、443 端口被独占了。若要想在一台VPS 上运行多个服务,还是有解决方案的,参考神器 https://ssldocker.com/ 375,下面是SSLDocker 配置示例:
{
"GzipOn": true,
"Http2https": true,
"MaxHeader": 10,
"Certs": "certs",
"ProxyItems": [
{"Host": "abc.com", "Target": "http://abc.com"},
{"Host": "app1.com", "Target": "http://127.0.0.1:1234"},
{"Host": "app2.com", "Target": "http://127.0.0.1:1235"}
]
}
用Supervisor 守护SSLDocker 和后台进程,原理图如下:
相关项目:
- autocert https://github.com/golang/crypto/tree/master/acme/autocert 34
- SSLDocker https://ssldocker.com/ 375
本文网址: https://golangnote.com/topic/211.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- Golang实现简单的Socks5代理
- goLang 实现排列组合的代码
- 用Go语言写一个最简单的echo服务器
- golang 用正则包regexp 通过user-agent 识别手机浏览器
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang 字符串毫秒转时间格式
- Golang 简单的任务队列
- Golang telegram 机器人小试
- Golang 定时循环的实现
- golang 用 crypto/bcrypt 存储密码的例子
- Golang 生成防识别的图片验证码
- golang 实现Authenticator 二次验证,可用到web 登录
- golang 为Windows XP/Server 2003 编译程序
- Golang io.ReadCloser 和[]byte 相互转化的方法
- groupcache 使用入门
- golang 用gzip 压缩、解压缩字符串
- chroma: 纯go 实现的类似Pygments 的代码高亮库
- golang flate/zlib 解压缩
- golang 生成良好的唯一ID/uuid库比较
- Golang 生成 session id