php Editor Zimo sometimes encounters a 502 Bad Gateway prompt when using the Internet. This error message means that there is a communication problem between the gateway server and the upstream server, resulting in the request not being completed. One of the possible causes is a net/http GET failure for the IPSec tunnel. Although this error can be confusing, by understanding its cause and how to fix it, we can quickly resolve the issue and continue enjoying the convenience of the Internet.
Issuing a remote get
request through ipsec tunnel (vpn) on windows
using the net/http
package When , the get
request failed and the response code was 502 bad gateway
. However, requests sent by curl
with the same headers
, uri
, method
work just fine. go
The source code is as follows:
var req *http.request req, err = http.newrequest('get', *uri, reader) if nil != err { level.warn(log).log("msg", "unable to create request", "err", err, "uri", uri) return nil, err } if nil != req.body { defer req.body.close() } for h, v := range opts.headers { req.header.add(h, *v) } client := http.client{} if nil != err { return nil, err } res, err := client.do(req) if nil != err { level.warn(log).log("msg", "unable to send request", "err", err, "uri", uri) return nil, err } defer res.body.close() buf, err := io.readall(res.body) if nil != err { level.warn(log).log("msg", "unable to read response", "err", err, "uri", uri, "status", res.status, "status_code", res.statuscode) return nil, err }
After analyzing the packet capture of the traffic, it was found that the vpn interface
can normally capture the request sent by curl
, but get
cannot be retrieved from go(net /http)
The request sent was lost.
Then I guess it may be because the framework chooses the wrong interface to send the packet.
So I try to find the correct local network interface and specify its localaddr
to send my get
request like this:
func Client(u string) (*http.Client, error) { uri, err := url.Parse(u) if nil != err { return nil, err } host := uri.Host port := uri.Port() if Empty == port { if uri.Scheme == "http" { host = host + ":80" } else if uri.Scheme == "https" { host = host + ":443" } } conn, err := net.Dial("tcp", host) if nil != err { return nil, err } addr := conn.LocalAddr().(*net.TCPAddr) // **make sure using random port as local port** addr.Port = 0 dialer := &net.Dialer{LocalAddr: addr} dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) { conn, err := dialer.Dial(network, addr) return conn, err } transport := &http.Transport{DialContext: dialContext} client := &http.Client{ Transport: transport, } return client, nil }
After replacing client
with the client
function, the get
request finally works.
client := &http.client{}
=> client, err := client(uri)
So my question is why golang
is not selecting the correct local network interface to automatically send the request?
After debuggingnet.dial#DialContext
, I found that the address has been changed to my Visual Studio Code
settings proxy address. My proxy server will not be able to handle my application requests.
When I remove the proxy settings, net.dial
works as expected.
I'm using vscode to work with TypeScript/Java/Golang...
and this is my first time developing an application in
vscode IDE
Encountering proxy issues. In other words, I never thought the IDE
agent could do the job it was built for. It makes sense since the extension or the IDE itself uses this proxy setting, but my golang
project debugging also uses it, which really confuses me.
The above is the detailed content of 502 Bad Gateway: net/http GET failed for IPSec tunnel. For more information, please follow other related articles on the PHP Chinese website!