Fer*_*lus 3 reverse-proxy ssl-certificate go
我用自己的ReverseProxy做了一些麻烦我用Go写的.我想将我的Golang-Webserver与我的Apache Webserver连接起来.我的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)
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多,并生成了几个自签名的SSL证书,但没有解决我的问题.希望有人可以帮助我.
问候
大卫
如果您在后端服务器中使用自签名证书,则需要告知代理的http客户端不验证证书.
您可以覆盖http包的默认值:
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
Run Code Online (Sandbox Code Playgroud)
或者专门为您的代理创建一个新的传输:
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},
}
Run Code Online (Sandbox Code Playgroud)