Home > Article > Backend Development > Go proxy fails when learning routes
php editor Strawberry understands the importance of routing when solving the problem of Go proxy failure. Routing is a core concept in network communications, which determines how data packets should be delivered from source address to destination address. When using Go language for proxying, it is very important to configure routing correctly. By deeply understanding the principles of routing and related configurations, we can effectively solve the problem of Go proxy failure and ensure the stability and reliability of network communication. In this article, we will introduce the working principle of routing and common configuration methods to help everyone better understand and apply routing technology.
I have a simple go proxy like this. I want to proxy requests through it and modify the responses of some websites. The sites run via tls, but my proxy is just a local server.
func main() { target, _ := url.parse("https://www.google.com") proxy := httputil.newsinglehostreverseproxy(target) proxy.modifyresponse = rewritebody http.handle("/", proxy) http.listenandserve(":80", proxy) }
Result: 404 error, as shown below:
As I understand it, the proxy server initiates the request and closes the request, then returns a modified response. I'm not sure it will fail here. Am I missing something to forward the headers to where this request fails?
I have the routing working properly. Initially, I was interested in modifying the response, but saw no changes other than seeing the magical
header.
func modifyResponse() func(*http.Response) error { return func(resp *http.Response) error { resp.Header.Set("X-Proxy", "Magical") b, _ := ioutil.ReadAll(resp.Body) b = bytes.Replace(b, []byte("About"), []byte("Modified String Test"), -1) // replace html body := ioutil.NopCloser(bytes.NewReader(b)) resp.Body = body resp.ContentLength = int64(len(b)) resp.Header.Set("Content-Length", strconv.Itoa(len(b))) resp.Body.Close() return nil } } func main() { target, _ := url.Parse("https://www.google.com") proxy := httputil.NewSingleHostReverseProxy(target) director := proxy.Director proxy.Director = func(r *http.Request) { director(r) r.Host = r.URL.Hostname() } proxy.ModifyResponse = modifyResponse() http.Handle("/", proxy) http.ListenAndServe(":80", proxy) }
The key issue is mentioned in the document, but it is not clear from the document how to deal with it accurately:
newsinglehostreverseproxy does not rewrite the host header. rewrite Host header, use reverseproxy directly with a custom director policy.
https://www.php.cn/link/747e32ab0fea7fbd2ad9ec03daa3f840
You are not using reverseproxy
directly. You can still use newsinglehostreverseproxy
and adjust the director
function like this:
func main() { target, _ := url.Parse("https://www.google.com") proxy := httputil.NewSingleHostReverseProxy(target) director := proxy.Director proxy.Director = func(r *http.Request) { director(r) r.Host = r.URL.Hostname() // Adjust Host } http.Handle("/", proxy) http.ListenAndServe(":80", proxy) }
The above is the detailed content of Go proxy fails when learning routes. For more information, please follow other related articles on the PHP Chinese website!