Error: socket hang up in axios Through a Proxy

The connection closed before the response arrived. Through a proxy, the cause is usually how axios sends an HTTPS request, or a port and protocol mismatch.

Updated · Prokso editorial team

Key takeaways
  • socket hang up means the connection closed before the response arrived
  • Old axios versions sent HTTPS requests to the proxy without a CONNECT tunnel, and the proxy dropped the connection
  • A reliable setup is https-proxy-agent or socks-proxy-agent with proxy: false and a matching port and protocol

What the error means

According to the Node.js documentation, when a connection closes before the response is received, the request fails with Error: socket hang up and the code ECONNRESET. In Node.js, axios runs on top of the http and https modules, so it shows the same error.

Through a proxy, what matters is how the client sends a request to an HTTPS address. The correct way is to ask the proxy to open a tunnel with the CONNECT method first and then set up TLS with the site inside that tunnel. In axios issue #4531 (version 0.26.1), the built-in proxy option sent the HTTPS request to the proxy without CONNECT, the proxy dropped the connection, and the program got socket hang up.

The axios 1.16.1 changelog (May 2026) lists fix #10858, and the README of the 1.x branch describes the new behavior: for https:// targets axios opens a CONNECT tunnel and performs TLS end-to-end with the site. So on old versions the error is usually fixed by changing how the proxy is connected, and on new ones by checking the port and protocol.

Causes

  • An old axios version. The built-in proxy option does not open a CONNECT tunnel for HTTPS targets, and the proxy closes the connection.
  • The protocol does not match the port. Port 823 accepts HTTP, port 824 accepts SOCKS5. The proxy cannot parse an HTTP request sent to port 824 and drops the connection.
  • protocol: 'https' for an HTTP port. The axios README says to set https in the proxy config only when the proxy server itself uses HTTPS. Otherwise the TLS handshake goes to a plain HTTP port.
  • An agent and the proxy option together. If you pass your own agent and http_proxy or https_proxy is set in the system, axios may also apply the proxy from the environment variables.
  • A drop on the site or device side. The site closed the connection, or the sticky IP changed because the device left the network.

How to fix it

  1. Check the version with npm ls axios. If it is older than 1.16.1, update axios or connect the proxy through an agent as in the next step.
  2. For an HTTP proxy, create an agent new HttpsProxyAgent('http://USERNAME:PASSWORD@gw.prokso.com:823') from the https-proxy-agent package, pass it as httpsAgent and add proxy: false. According to the README, axios then leaves tunneling to the agent, and proxy: false disables proxies from environment variables.
  3. For SOCKS5, use the socks-proxy-agent package: pass new SocksProxyAgent('socks5h://USERNAME:PASSWORD@gw.prokso.com:824') as both httpAgent and httpsAgent, also with proxy: false.
  4. If you keep the built-in proxy option, set protocol: 'http', the host gw.prokso.com and port 823, or a port from 10000-20000 for a sticky IP. A full example is in the Node.js guide.
  5. If the error is rare and appears in the middle of a job, add a retry with a pause. More on dropped connections is on the ECONNRESET and ECONNREFUSED page.

How to check that it works

First test the proxy string itself in the proxy checker. Then make an axios request to https://prokso.com/ip: the response should be JSON with the proxy IP and country, not your own address. If the error remains, go through the step-by-step check.

Two-minute diagnosis

First paste the line into the proxy checker. If the proxy works there, the cause is in your app or its settings. If not, test the line from a terminal:

curl -v -x "http://USERNAME:PASSWORD@gw.prokso.com:823" https://api.ipify.org
  • A < HTTP/1.1 407 line right after CONNECT means the proxy answered. The name after the code, for example NO_USER, points to the cause.
  • The proxy answered CONNECT with 200 and then another code arrived - the proxy works, and that answer came from the website.
  • No answer at all, only a timeout - the connection never reached the proxy: check the host, port and network.

Setup guides where this error comes up

FAQ

Why does curl work while axios does not?

curl opens a CONNECT tunnel for HTTPS targets. An old axios version with the built-in proxy option did not, so the same proxy works in curl and drops the connection in axios.

Do I have to set proxy: false together with an agent?

Yes, if http_proxy or https_proxy may be set in the system. The axios README says custom agents without proxyEnv still go through axios environment proxy resolution, and proxy: false turns it off.

Does the axios proxy option support SOCKS5?

The README describes the option for HTTP and HTTPS proxies. For SOCKS5, connect socks-proxy-agent as an agent.

I got a 407 code instead of socket hang up. What is it?

The proxy received the request but did not accept the username or password. See the <a href="/en/errors/407-proxy-authentication-required">407 error</a>.

Residential $1.59, mobile $2.50, datacenter $0.65 per GB

Sign up in a minute, get access right after payment.

Create account