【问题标题】:How to use TLS in Go ReverseProxy?如何在 Go ReverseProxy 中使用 TLS?
【发布时间】:2018-01-15 11:18:39
【问题描述】:

我的站点 https://a-b-c.comhttps://www.x-y-z.com 分别在端口 44445555 上的反向代理后面运行。

我已将它们配置为使用letsencrypt tls证书,但现在使用反向代理时出现错误,我认为我需要使用包含其证书的&tls.config{},但我不知道如何设置它.

我的 ReverseProxy 看起来像:

  director := func(req *http.Request) {                        
    log.Println(req.Host)                                      
    switch req.Host {                                          
    case "a-b-c-.com":                                 
      req.URL.Host = "localhost:4444"                        
      req.URL.Scheme = "https"                                 
    case "x-y-z.com":                                   
      req.URL.Host = "localhost:5555"                           
      req.URL.Scheme = "https"                                                                                   
  }                                                            
  proxy := &httputil.ReverseProxy{Director: director}          
  proxy.Transport = &http.Transport{                           
    Proxy: http.ProxyFromEnvironment,                          
    Dial: (&net.Dialer{                                        
      Timeout:   30 * time.Second,                             
      KeepAlive: 30 * time.Second,                             
    }).Dial,                                                   
    TLSHandshakeTimeout: 10 * time.Second,                     
    TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
  }                                                            

  log.Fatalln(http.ListenAndServe(":443", proxy))              

【问题讨论】:

    标签: ssl go reverse-proxy


    【解决方案1】:

    您不必设置反向代理。您需要做的是设置您附加反向代理的 HTTP 服务器。

    通过传递两对证书/密钥来配置 TLS 监听器:

    abcCrt, err := tls.LoadX509KeyPair("a-b-c.crt", "a-b-c.key")
    if err != nil {
      panic(err)
    }
    
    xyzCrt, err := tls.LoadX509KeyPair("x-y-z.crt", "z-y-z.key")
    if err != nil {
      panic(err)
    }
    
    tlsConfig := &tls.Config{Certificates: []tls.Certificate{abcCrt, xyzCrt}}
    ln, err := tls.Listen("tcp", ":443", tlsConfig) 
    if err != nil {
      panic(err)
    }
    
    http.Serve(ln, proxy)
    

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 2021-10-06
      • 2019-08-20
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2016-04-22
      相关资源
      最近更新 更多