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 remoteget
request throughipsec tunnel (vpn) on windows
using thenet/http
package When , theget
request failed and the response code was502 bad gateway
. However, requests sent bycurl
with the sameheaders
,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 thevpn interface
can normally capture the request sent bycurl
, butget
cannot be retrieved fromgo(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 itslocaladdr
to send myget
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 replacingclient
with theclient
function, theget
request finally works.
client := &http.client{}
=>client, err := client(uri)
So my question is whygolang
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 myVisual 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 withTypeScript/Java/Golang...
and this is my first timedeveloping an application in
vscodeIDE
Encountering proxy issues. In other words, I never thought theIDE
agent could do the job it was built for. It makes sense since the extension or the IDE itself uses this proxy setting, but mygolang
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!