The reverseproxy package provides an HTTP reverse proxy implementation specifically designed for proxying requests to Unix sockets (such as the Docker socket). It is a simplified version of Go's net/http/httputil.ReverseProxy optimized for socket proxying use cases.
Key Characteristics
- Director-only: Only the
Director function is supported for request modification. The Rewrite and ModifyResponse hooks from the standard library are not available. - Context-aware streaming: Uses
ioutils.CopyCloseWithContext to respect request cancellation, use Content-Length for optimal copying, and handle trailer headers. - No buffering: Streams responses directly to the client without the buffering behavior found in the standard library.
// Example of initializing a ReverseProxy for a Unix socket
rp := &reverseproxy.ReverseProxy{
Director: func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = "api.moby.localhost"
req.RequestURI = req.URL.String()
},
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return net.DialTimeout("unix", "/var/run/docker.sock", 5*time.Second)
},
DisableCompression: true,
},
}
http.HandleFunc("/", rp.ServeHTTP)
http.ListenAndServe(":2375", nil)