Go - ReverseProxy to Apache proxy error: x509: certificate signed by unknown authority

Go - ReverseProxy to Apache proxy error: x509: certificate signed by unknown authority

我用 Go 编写的 ReverseProxy 遇到了一些问题。我想将我的 Golang-Web 服务器与我的 Apache Web 服务器连接起来。我的 Apache Web 服务器应该在 https 和反向代理上 运行。所以我写了下面的代码,但我总是得到错误:代理错误:x509:由未知权威签署的证书。 那么 apache 必须使用与 apache 相同的证书还是什么问题?这里有一些代码片段,但我认为没有 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 证书,但没有解决我的问题。 希望有人能帮助我。

问候

大卫

如果您在后端服务器中使用自签名证书,您需要告诉代理的 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},
}