Home>Article>Backend Development> golang reverse proxy cannot be accessed

golang reverse proxy cannot be accessed

王林
王林 Original
2020-03-12 11:26:09 2746browse

golang reverse proxy cannot be accessed

First, let’s take a look at how to get a reverse proxy structure

func NewSingleHostReverseProxy(target *url.URL) *ReverseProxy { targetQuery := target.RawQuery director := func(req *http.Request) { req.URL.Scheme = target.Scheme req.URL.Host = target.Host req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) if targetQuery == "" || req.URL.RawQuery == "" { req.URL.RawQuery = targetQuery + req.URL.RawQuery } else { req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery } if _, ok := req.Header["User-Agent"]; !ok { req.Header.Set("User-Agent", "") } } return &ReverseProxy{Director: director} }

NewSingleHostReverseProxy uses a closure as the Director to create a new ReverseProxy structure, and the director is It is the proxy function we implement by default. In this function, the protocol, Host and Path of the request URL are rewritten. But it does not rewrite req.Host, which results in our reverse proxy only being able to proxy locally. So we only need to modify this function. There are two ways to modify it.

(Recommended tutorial:golang tutorial)

Modification method:

1. We can copy the NewSingleHostReverseProxy function and modify it. Anyway, this function is also It's not complicated, and then use our custom function to create a new proxy where the reverse proxy structure is needed.

func NewProxy(target *url.URL) *httputil.ReverseProxy { targetQuery := target.RawQuery director := func(req *http.Request) { req.Host = target.Host // -- 加入这句 -- req.URL.Scheme = target.Scheme req.URL.Host = target.Host req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) if targetQuery == "" || req.URL.RawQuery == "" { req.URL.RawQuery = targetQuery + req.URL.RawQuery } else { req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery } if _, ok := req.Header["User-Agent"]; !ok { // explicitly disable User-Agent so it's not set to default value req.Header.Set("User-Agent", "") } } return &httputil.ReverseProxy{Director: director} }

Then replace httputil.NewSingleHostReverseProxy(u) with NewProxy(u) to proxy to other domain names.

2. Customize Director function. We still use the NewSingleHostReverseProxy function to create a new proxy, and then customize a Director function for it.

p := httputil.NewSingleHostReverseProxy(u) d := p.Director p.Director = func(r *http.Request) { d(r) r.Host = u.Host }

Recommended related video tutorials:golang video tutorial

The above is the detailed content of golang reverse proxy cannot be accessed. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn