From f0b1edaf8c118407793bc7efda3fc00409023836 Mon Sep 17 00:00:00 2001 From: Luit van Drongelen Date: Sat, 21 Nov 2015 20:00:44 +0100 Subject: [PATCH 1/2] Fix proxy for websocket with altered Host header --- middleware/proxy/reverseproxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/middleware/proxy/reverseproxy.go b/middleware/proxy/reverseproxy.go index e5652fa91..f9da992ec 100644 --- a/middleware/proxy/reverseproxy.go +++ b/middleware/proxy/reverseproxy.go @@ -169,7 +169,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, extr } defer conn.Close() - backendConn, err := net.Dial("tcp", outreq.Host) + backendConn, err := net.Dial("tcp", outreq.URL.Host) if err != nil { return err } From 1fb66d534a0c08f8f2cee6112d7528017e26b94c Mon Sep 17 00:00:00 2001 From: Luit van Drongelen Date: Sat, 21 Nov 2015 20:03:46 +0100 Subject: [PATCH 2/2] Close proxy's backend request earlier when re-connecting for websocket --- middleware/proxy/reverseproxy.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/middleware/proxy/reverseproxy.go b/middleware/proxy/reverseproxy.go index f9da992ec..90e546670 100644 --- a/middleware/proxy/reverseproxy.go +++ b/middleware/proxy/reverseproxy.go @@ -155,9 +155,9 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, extr if err != nil { return err } - defer res.Body.Close() if res.StatusCode == http.StatusSwitchingProtocols && res.Header.Get("Upgrade") == "websocket" { + res.Body.Close() hj, ok := rw.(http.Hijacker) if !ok { return nil @@ -182,6 +182,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request, extr }() io.Copy(conn, backendConn) // read tcp stream from backend. } else { + defer res.Body.Close() for _, h := range hopHeaders { res.Header.Del(h) }