问题描述
如何使用多个网域ListenAndServeTLS?我看到该函数接受一个证书和密钥文件,但我相信密钥文件可能只包含一个私钥。我有几个私钥,用于不同的证书链。
http.ListenAndServeTLS
是为了呈现最简单的配置。如果您想添加其他选项,可以使用自定义 tls.Config
创建 http.Server
。然后,您可以手动映射 tls.Config.NameToCertificate
中的名称,或者调用 BuildNameToCertificate()
以编程方式构建映射。
但是,您仍然可以使用 Server.ListenAndServeTLS
,因为它会将配置中的证书加载到通过方法参数传入的证书。
cfg:=& tls.Config {}
cert,err:= tls.LoadX509KeyPair(cert_one.pem,key_one.pem)
if err!= nil {
log.Fatal(err)
}
cfg.Certificates = append(cfg.Certificates,cert)
//继续向cfg.Certificates添加剩余的证书
cfg.BuildNameToCertificate()
服务器:= http.Server {
Addr:127.0.0.1:443,
Handler:myHandler,
TLSConfig:cfg,
}
server.ListenAndServeTLS(,)
How do I ListenAndServeTLS with multiple domains? I see the function accepts a cert and key file, but I believe the key file may only contain a single private key. I have a few private keys, for different certificate chains.
http.ListenAndServeTLS
is meant to be present a bare minimal configuration. If you want to add other options, you can create an http.Server
with a custom tls.Config
. You can then either manually map names in tls.Config.NameToCertificate
, or call BuildNameToCertificate()
to build the map programatically.
You can still use Server.ListenAndServeTLS
however, since it will load the certs in the config as well a cert passed in via the methods args.
cfg := &tls.Config{}
cert, err := tls.LoadX509KeyPair("cert_one.pem", "key_one.pem")
if err != nil {
log.Fatal(err)
}
cfg.Certificates = append(cfg.Certificates, cert)
// keep adding remaining certs to cfg.Certificates
cfg.BuildNameToCertificate()
server := http.Server{
Addr: "127.0.0.1:443",
Handler: myHandler,
TLSConfig: cfg,
}
server.ListenAndServeTLS("", "")
这篇关于带有多个证书的http.ListenAndServeTLS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!