Home >Backend Development >Golang >How to use net/http/httputil.NewSingleHostReverseProxy in golang to implement a simple reverse proxy
How to use net/http/httputil.NewSingleHostReverseProxy in golang to implement a simple reverse proxy
1. Introduction
In modern Internet applications, reverse proxy The agent is a very important component. It enables load balancing and improves application scalability and reliability. Golang's standard library provides a very convenient method httputil.NewSingleHostReverseProxy, which can implement the reverse proxy function simply and quickly. This article will introduce how to use this method to implement a simple reverse proxy through detailed code examples.
2. Principle of reverse proxy
Reverse proxy is the middle layer between the client and the server. The client sends a request to the reverse proxy server, and the reverse proxy server then proxies the request to Processed on the actual server. The communication between the client and the server is done through a reverse proxy server, thus achieving decoupling between the client and the server.
3. Use httputil.NewSingleHostReverseProxy to implement reverse proxy
import ( "net/http" "net/http/httputil" )
func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy
The sample code is as follows:
targetURL, _ := url.Parse("http://api.example.com") proxy := httputil.NewSingleHostReverseProxy(targetURL)
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { response, err := http.Get("http://localhost:8080") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer response.Body.Close() // 将原始请求的header复制到响应中 for key, values := range response.Header { for _, value := range values { w.Header().Add(key, value) } } // 将原始请求的body复制到响应中 _, err = io.Copy(w, response.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
http.HandleFunc("/", handler.ServeHTTP)
http.ListenAndServe(":8080", nil)
4. Sample code
The following is a complete sample code that proxies requests from localhost:8080 to api.example.com.
package main import ( "io" "net/http" "net/http/httputil" "net/url" ) type Handler struct{} func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { targetURL, _ := url.Parse("http://api.example.com") proxy := httputil.NewSingleHostReverseProxy(targetURL) proxy.ServeHTTP(w, r) } func main() { handler := &Handler{} http.HandleFunc("/", handler.ServeHTTP) http.ListenAndServe(":8080", nil) }
The above code can be run by executing go run main.go, and then visit http://localhost:8080 in the browser to see the effect of the proxy.
5. Summary
This article introduces how to use the httputil.NewSingleHostReverseProxy method in golang to implement a simple reverse proxy. With a few simple steps, we can quickly and easily build a reverse proxy server to implement request forwarding and load balancing. Reverse proxy is a very important part of Internet applications. Knowing how to use related tools and techniques will be very helpful for developing and maintaining large distributed systems. I hope this article has inspired you, thank you for reading!
The above is the detailed content of How to use net/http/httputil.NewSingleHostReverseProxy in golang to implement a simple reverse proxy. For more information, please follow other related articles on the PHP Chinese website!