【问题标题】:Go - ReverseProxy to Apache proxy error: x509: certificate signed by unknown authorityGo - ReverseProxy 到 Apache 代理错误:x509:证书由未知机构签名
【发布时间】:2016-03-20 01:57:42
【问题描述】:

我用 Go 编写的 ReverseProxy 遇到了一些问题。我想将我的 Golang-Webserver 与我的 Apache Webserver 连接起来。我的 Apache 网络服务器也应该在 https 和反向代理上运行。所以我写了以下代码,但我总是收到错误:代理错误:x509:证书由未知机构签名。 那么apache必须使用与apache相同的证书还是有什么问题?这里有一些代码sn-ps,但我认为没有ssl的证书有问题一切正常:(

func (p *Proxy) directorApache(req *http.Request) {
    mainServer := fmt.Sprintf("%s:%d", Config.HostMain, Config.PortMain)
    req.URL.Scheme = "https"
    req.URL.Host = mainServer
}
func (p *Proxy) directorGo(req *http.Request) {
    goServer := fmt.Sprintf("%s:%d", Config.GoHost, Config.GoPort)
    req.URL.Scheme = "http"
    req.URL.Host = goServer
}


func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    fmt.Println(req.URL.Path)
    if p.isGoRequest(req) {
        fmt.Println("GO")
        p.goProxy.ServeHTTP(rw, req)
        return
    }
    p.httpProxy.ServeHTTP(rw, req)
}
func main() {

    var configPath = flag.String("conf", "./configReverse.json", "Path to the Json config file.")

    flag.Parse()
    proxy := New(*configPath)

    cert, err := tls.LoadX509KeyPair(Config.PathCert, Config.PathPrivateKey)
    if err != nil {
        log.Fatalf("server: loadkeys: %s", err)
    }
    config := tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}}

    listener, err := net.Listen("tcp",
    net.JoinHostPort(proxy.Host, strconv.Itoa(proxy.Port)))
    if err != nil {
        log.Fatalf("server: listen: %s", err)
    }
    log.Printf("server: listening on %s")
    proxy.listener = tls.NewListener(listener, &config)

    serverHTTPS := &http.Server{
        Handler:   proxy.mux,
        TLSConfig: &config,
    }

    if err := serverHTTPS.Serve(proxy.listener); err != nil {
        log.Fatal("SERVER ERROR:", err)
    }

   }

我尝试了很多并生成了几个自签名 SSL 证书,但没有解决我的问题。 希望有人可以帮助我。

问候

大卫

【问题讨论】:

  • 什么是返回证书错误? httpProxy 是什么?如果 apache 在 go server 后面,那为什么是 https 呢?
  • p.httpProxy = &httputil.ReverseProxy{ Director: p.directorApache, } 因为如果有人直接连接到我的 apache。如果我使用反向代理作为 https 和 apache 作为 http,那么由于混合了 https 和 http,我会得到混合内容的错误......
  • 是返回证书错误的代理吗? apache 是否有受信任的 CA 签署的证书?您在侦听器上设置 InsecureSkipVerify: true,但这是一个客户端设置(尽管您根本不需要使用有效证书)。
  • 我认为代理返回错误作为响应来自 Apache。我使用教程为 localhost 生成了一个自签名证书,因为我是 SSL 和这些证书的新手。我如何验证它是否是有效的证书或如何获得有效的证书?哦,好的,谢谢,我不知道。
  • 我建议不要直接使用 apache,也不要通过反向代理对同一个项目使用,因为它只会使架构复杂化。如果您出于某种原因需要直接连接到 apache,请将其提供的所有资源都连接到它,或者甚至将其放在您的 go 服务器前面。

标签: go ssl-certificate reverse-proxy


【解决方案1】:

如果您在后端服务器中使用自签名证书,则需要告诉代理的 http 客户端不要验证该证书。

您可以覆盖 http 包的默认值:

http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

或者专门为您的代理创建一个新的传输:

httpProxy.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},
}

【讨论】:

  • 非常感谢。第一个解决方案很好地解决了我的问题:)
猜你喜欢
  • 2019-03-07
  • 1970-01-01
  • 2020-03-16
  • 2023-01-09
  • 2017-12-23
  • 2021-02-01
  • 2020-05-27
  • 2021-09-23
  • 1970-01-01
相关资源
最近更新 更多