我正在努力制作自己的ReverseProxy。 所以它应该将我的Apache Web服务器与另一个Go Web服务器连接起来。
因此,当我请求“/”时,代理应该将请求发送到apache并为apache服务器的网站提供服务。 当我请求“/ gotest”时,它应该将请求发送到go webserver
但是ReverseProxy和Apache Webserver应该在不同的ip-adresses上运行。但是所有这些都应该运行SSL。 示例:ReverseProxy运行于:192.168.2.1 Apache Server运行于:192.168.2.5
如果两者都在相同的IP但不同的端口上运行,那么它运行良好。 但是当我在不同的ip上运行Apache时,reverseeproxy会收到一个请求并显示网站。但是后来我的反转手术没有得到新的要求。
有谁知道我做错了什么?
type Proxy struct {
listener net.Listener
TlsConfig *tls.Config //config of the TLS-Connection
mux *http.ServeMux
goHandler http.Handler //Handler to send Requests to GO-Webserver
mainHandler http.Handler //Handler to send Requests to the main webserver
Host string //Host of the Reverse-Proxy
Port int //Port of the Reverse-Proxy
}
//New returns a new ReverseProxy with the configuration of the given toml file
func New(configPath string) *Proxy {
if err := common.READCONFIG(configPath); err != nil {
log.Fatalf(err.Error())
}
p := &Proxy{
mux: http.NewServeMux(),
Host: common.CONFIG.ReverseProxy.Host,
Port: common.CONFIG.ReverseProxy.Port,
}
p.goHandler = &httputil.ReverseProxy{
Director: p.directorGo,
FlushInterval: 1 * time.Millisecond,
}
p.mainHandler = &httputil.ReverseProxy{
Director: p.directorMain,
FlushInterval: 1 * time.Millisecond,
}
pathCert := common.CONFIG.PathCert
pathKey := common.CONFIG.PathPrivateKey
//Loads the ssl-cert and private-keyn
cert, err := tls.LoadX509KeyPair(pathCert, pathKey)
if err != nil {
common.Log.Critical("ReverseProxy: load SSL-Keys: %s", err)
}
//Change Transport to avoid ssl-certificate error:"certificate signed by unknown authority"
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
p.TlsConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
listener, err := net.Listen("tcp",
net.JoinHostPort(p.Host, strconv.Itoa(p.Port)))
if err != nil {
common.Log.Critical("ReverseProxy: can't listen: %s", err)
os.Exit(1)
}
p.listener = tls.NewListener(listener, p.TlsConfig)
//Each request should be handled by the proxy
p.mux.Handle("/", p)
return p
}
//isGoRequest checks if the given Request is for the go-webserver
func (p *Proxy) getProxyAdress() string {
return fmt.Sprintf("%s:%d", p.Host, p.Port)
}
//isGoRequest checks if the given Request is for the go-webserver
func (p *Proxy) isGoRequest(req *http.Request) bool {
if strings.Contains(req.URL.Path, common.CONFIG.ReverseProxy.GoUrlPath) {
return true
} else {
return false
}
}
//directorMain modifies the request to a new request for the main-webserver
func (p *Proxy) directorMain(req *http.Request) {
mainServer := fmt.Sprintf("%s:%d", common.CONFIG.ReverseProxy.HostMain, common.CONFIG.ReverseProxy.PortMain)
req.URL.Scheme = "https"
req.URL.Host = mainServer
}
//directorGo modifies the request to a new request for the go-webserver
func (p *Proxy) directorGo(req *http.Request) {
goServer := fmt.Sprintf("%s:%d", common.CONFIG.Pointstreamer.Host, common.CONFIG.Pointstreamer.Port)
req.URL.Scheme = "https"
req.URL.Host = goServer
}
// ServeHTTP implements the http.Handler interface.
func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Println(req.URL.Path)
if p.isGoRequest(req) {
p.goHandler.ServeHTTP(rw, req)
return
}
p.mainHandler.ServeHTTP(rw, req)
}
func main() {
var configPath = flag.String("conf", "../../configDevel.toml", "Path to the toml config file.")
flag.Parse()
proxy := New(*configPath)
httpsServer := &http.Server{
Handler: proxy.mux,
TLSConfig: proxy.TlsConfig,
}
common.Log.Info("Server listens on", proxy.getProxyAdress(), "Main Redirect", common.CONFIG.ReverseProxy.HostMain)
if err := httpsServer.Serve(proxy.listener); err != nil {
common.Log.Critical("ReverseProxy Server Error:", err)
os.Exit(1)
}
}