在golang中使用net/http是否可以托管多个域名的TLS?

huangapple go评论103阅读模式
英文:

Is it possible to host multiple domain TLS in golang with net/http?

问题

我有多个域名(比如abc.com和xyz.org),每个域名都有不同的证书。是否可以在不深入底层和使用net.Listen等的情况下,根据主机名使用密钥和证书?只是使用简单的http.ListenAndServeTLS(...)或类似的方法?基本上就像nginx所做的那样。

英文:

I have multiple domain (let's say abc.com and xyz.org) with diffrent certificate. Is it possible to use key and certificate based on hostname without going deep low level and net.Listen, etc. Just using simple http.ListenAndServeTLS(...) or similar ?
Basically like what nginx does.

答案1

得分: 23

BuildNameToCertificate()函数将从证书中提取主机名。如果没有匹配的SNI信息,它将返回[0]。

更新至Go 1.14 - 请参阅https://github.com/golang/go/commit/eb93c684d40de4924fc0664d7d9e98a84d5a100b

  1. package main
  2. import (
  3. "crypto/tls"
  4. "net/http"
  5. "time"
  6. "log"
  7. )
  8. func myHandler(w http.ResponseWriter, r *http.Request) {
  9. w.Write([]byte("tls"))
  10. }
  11. func main() {
  12. t := log.Logger{}
  13. var err error
  14. tlsConfig := &tls.Config{}
  15. tlsConfig.Certificates = make([]tls.Certificate, 3)
  16. // go http server treats the 0'th key as a default fallback key
  17. tlsConfig.Certificates[0], err = tls.LoadX509KeyPair("test0.pem", "key.pem")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. tlsConfig.Certificates[1], err = tls.LoadX509KeyPair("test1.pem", "key.pem")
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. tlsConfig.Certificates[2], err = tls.LoadX509KeyPair("test2.pem", "key.pem")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. // as of go 1.14 this line is no longer needed
  30. // load the certs as above and skip BuildNameToCertificate()
  31. tlsConfig.BuildNameToCertificate()
  32. http.HandleFunc("/", myHandler)
  33. server := &http.Server{
  34. ReadTimeout: 10 * time.Second,
  35. WriteTimeout: 10 * time.Second,
  36. MaxHeaderBytes: 1 << 20,
  37. TLSConfig: tlsConfig,
  38. }
  39. listener, err := tls.Listen("tcp", ":8443", tlsConfig)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. log.Fatal(server.Serve(listener))
  44. }

以上是要翻译的内容。

英文:

BuildNameToCertificate() will sniff the hostname from the cert. If none match the SNI info it serves the [0].
https://golang.org/src/crypto/tls/common.go?s=18204:18245#L947

Update for Go 1.14 - see https://github.com/golang/go/commit/eb93c684d40de4924fc0664d7d9e98a84d5a100b

  1. package main
  2. import (
  3. &quot;crypto/tls&quot;
  4. &quot;net/http&quot;
  5. &quot;time&quot;
  6. &quot;log&quot;
  7. )
  8. func myHandler(w http.ResponseWriter, r *http.Request) {
  9. w.Write([]byte(&quot;tls&quot;))
  10. }
  11. func main() {
  12. t := log.Logger{}
  13. var err error
  14. tlsConfig := &amp;tls.Config{}
  15. tlsConfig.Certificates = make([]tls.Certificate, 3)
  16. // go http server treats the 0&#39;th key as a default fallback key
  17. tlsConfig.Certificates[0], err = tls.LoadX509KeyPair(&quot;test0.pem&quot;, &quot;key.pem&quot;)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. tlsConfig.Certificates[1], err = tls.LoadX509KeyPair(&quot;test1.pem&quot;, &quot;key.pem&quot;)
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. tlsConfig.Certificates[2], err = tls.LoadX509KeyPair(&quot;test2.pem&quot;, &quot;key.pem&quot;)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. // as of go 1.14 this line is no longer needed
  30. // load the certs as above and skip BuildNameToCertificate()
  31. tlsConfig.BuildNameToCertificate()
  32. http.HandleFunc(&quot;/&quot;, myHandler)
  33. server := &amp;http.Server{
  34. ReadTimeout: 10 * time.Second,
  35. WriteTimeout: 10 * time.Second,
  36. MaxHeaderBytes: 1 &lt;&lt; 20,
  37. TLSConfig: tlsConfig,
  38. }
  39. listener, err := tls.Listen(&quot;tcp&quot;, &quot;:8443&quot;, tlsConfig)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. log.Fatal(server.Serve(listener))
  44. }

huangapple
  • 本文由 发表于 2016年2月26日 09:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/35641888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定