Go – 反向代理到 Apache 代理错误:x509:由未知机构签名的证书

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

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

问题

我有一些关于我自己编写的Go ReverseProxy 的问题。我想要将我的 Golang Web 服务器与 Apache Web 服务器连接起来。我的 Apache Web 服务器应该在 https 上运行,反向代理也是如此。所以我写了以下代码,但是我总是得到错误:proxy error: x509: certificate signed by unknown authority(代理错误:x509:证书由未知机构签名)。所以是不是 Apache 必须使用与 Apache 相同的证书,或者问题是什么?这里是一些代码片段,但我认为问题是证书的问题,没有 SSL 时一切都正常工作 Go – 反向代理到 Apache 代理错误:x509:由未知机构签名的证书

  1. func (p *Proxy) directorApache(req *http.Request) {
  2. mainServer := fmt.Sprintf("%s:%d", Config.HostMain, Config.PortMain)
  3. req.URL.Scheme = "https"
  4. req.URL.Host = mainServer
  5. }
  6. func (p *Proxy) directorGo(req *http.Request) {
  7. goServer := fmt.Sprintf("%s:%d", Config.GoHost, Config.GoPort)
  8. req.URL.Scheme = "http"
  9. req.URL.Host = goServer
  10. }
  11. func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  12. fmt.Println(req.URL.Path)
  13. if p.isGoRequest(req) {
  14. fmt.Println("GO")
  15. p.goProxy.ServeHTTP(rw, req)
  16. return
  17. }
  18. p.httpProxy.ServeHTTP(rw, req)
  19. }
  20. func main() {
  21. var configPath = flag.String("conf", "./configReverse.json", "Path to the Json config file.")
  22. flag.Parse()
  23. proxy := New(*configPath)
  24. cert, err := tls.LoadX509KeyPair(Config.PathCert, Config.PathPrivateKey)
  25. if err != nil {
  26. log.Fatalf("server: loadkeys: %s", err)
  27. }
  28. config := tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}}
  29. listener, err := net.Listen("tcp", net.JoinHostPort(proxy.Host, strconv.Itoa(proxy.Port)))
  30. if err != nil {
  31. log.Fatalf("server: listen: %s", err)
  32. }
  33. log.Printf("server: listening on %s")
  34. proxy.listener = tls.NewListener(listener, &config)
  35. serverHTTPS := &http.Server{
  36. Handler: proxy.mux,
  37. TLSConfig: &config,
  38. }
  39. if err := serverHTTPS.Serve(proxy.listener); err != nil {
  40. log.Fatal("SERVER ERROR:", err)
  41. }
  42. }

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

问候,

David

英文:

i have some troubles with my own ReverseProxy i've written in Go. I want to connect my Golang-Webserver with my Apache Webserver. My Apache Webserver should be running on https and the Reverse-Proxy too. So i've written following code, but I always get the error: proxy error: x509: certificate signed by unknown authority.
So must the apache uses the same certificate as the apache or what is the problem? Here some code snippets but I think its a problem with the certificates without ssl everything works fine Go – 反向代理到 Apache 代理错误:x509:由未知机构签名的证书

  1. func (p *Proxy) directorApache(req *http.Request) {
  2. mainServer := fmt.Sprintf("%s:%d", Config.HostMain, Config.PortMain)
  3. req.URL.Scheme = "https"
  4. req.URL.Host = mainServer
  5. }
  6. func (p *Proxy) directorGo(req *http.Request) {
  7. goServer := fmt.Sprintf("%s:%d", Config.GoHost, Config.GoPort)
  8. req.URL.Scheme = "http"
  9. req.URL.Host = goServer
  10. }
  11. func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
  12. fmt.Println(req.URL.Path)
  13. if p.isGoRequest(req) {
  14. fmt.Println("GO")
  15. p.goProxy.ServeHTTP(rw, req)
  16. return
  17. }
  18. p.httpProxy.ServeHTTP(rw, req)
  19. }
  20. func main() {
  21. var configPath = flag.String("conf", "./configReverse.json", "Path to the Json config file.")
  22. flag.Parse()
  23. proxy := New(*configPath)
  24. cert, err := tls.LoadX509KeyPair(Config.PathCert, Config.PathPrivateKey)
  25. if err != nil {
  26. log.Fatalf("server: loadkeys: %s", err)
  27. }
  28. config := tls.Config{InsecureSkipVerify: true, Certificates: []tls.Certificate{cert}}
  29. listener, err := net.Listen("tcp",
  30. net.JoinHostPort(proxy.Host, strconv.Itoa(proxy.Port)))
  31. if err != nil {
  32. log.Fatalf("server: listen: %s", err)
  33. }
  34. log.Printf("server: listening on %s")
  35. proxy.listener = tls.NewListener(listener, &config)
  36. serverHTTPS := &http.Server{
  37. Handler: proxy.mux,
  38. TLSConfig: &config,
  39. }
  40. if err := serverHTTPS.Serve(proxy.listener); err != nil {
  41. log.Fatal("SERVER ERROR:", err)
  42. }
  43. }

I tried a lot and generated several self-signed SSL-Certificates but nothing solved my problem.
Hope somebody can help me.

greetings

David

答案1

得分: 12

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

您可以覆盖http包的默认设置:

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

或者为您的代理创建一个新的Transport:

  1. httpProxy.Transport = &http.Transport{
  2. Proxy: http.ProxyFromEnvironment,
  3. Dial: (&net.Dialer{
  4. Timeout: 30 * time.Second,
  5. KeepAlive: 30 * time.Second,
  6. }).Dial,
  7. TLSHandshakeTimeout: 10 * time.Second,
  8. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  9. }
英文:

If you're using a self-signed certificate in the backend server, you need to tell your proxy's http client to not verify the certificate.

You can override the default for the http package:

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

Or create a new Transport specifically for your proxy:

  1. httpProxy.Transport = &http.Transport{
  2. Proxy: http.ProxyFromEnvironment,
  3. Dial: (&net.Dialer{
  4. Timeout: 30 * time.Second,
  5. KeepAlive: 30 * time.Second,
  6. }).Dial,
  7. TLSHandshakeTimeout: 10 * time.Second,
  8. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  9. }

huangapple
  • 本文由 发表于 2015年12月14日 22:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/34269540.html
匿名

发表评论

匿名网友

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

确定