## TL;DR
Cloudflare is closing the TCP connection mid-request, usually because of connection-reuse mismatch, too-aggressive keep-alive, or request patterns it dislikes. Create a fresh axios instance with keep-alive tuned down, set a real user agent, and pace requests. If a clean, slow, identified client still gets hung up on, Cloudflare is shedding your traffic and you should stop or use an official feed.

```text
axios error socket hang up scraping cdn endpoint behind cloudflare
```

## Use this when
- axios throws `socket hang up` on Cloudflare-fronted hosts but works elsewhere.
- The failures cluster after many requests on one agent instance.
- Browsers load the same URLs fine.

## Not for this skill when
- The response is a 403 challenge page. That is a bot decision, not a socket problem.
- Every host fails, Cloudflare or not. That is your network.

## Steps
1. Disable or tune keep-alive: use `https.Agent({ keepAlive: false })` on the axios instance as a test. Verify: hang ups stop, confirming stale-socket reuse.
2. If you need keep-alive for speed, set `keepAlive: true` with a short `timeout` on the agent (e.g. 30s) so dead sockets recycle. Verify: hang ups drop without losing all reuse benefit.
3. Send a real browser user agent and accept headers. Default axios headers are a bot signal and Cloudflare sheds obvious bots at the TCP layer. Verify: identified requests get further.
4. Pace the requests: add delays and cap concurrency. Verify: the hang-up rate falls as the rate falls.
5. If hang ups persist on a clean, slow, identified client, Cloudflare is refusing your traffic. Stop scraping that host or find an official API. Verify: the scraper no longer hits the host.

## Variant phrasings
### axios socket hang up cloudflare
The short search, same keep-alive diagnosis.
### ECONNRESET scraping cloudflare cdn
Error-code phrasing, points at step 1.
### axios cloudflare connection closed
Symptom phrasing, lands on the pacing check.

Compatibility: axios 1.x on Node. The hang up is TCP-level, so axios version matters less than agent config.

## Why it happens
A socket hang up means the server closed the connection without a response. Cloudflare does this to shed traffic it does not want: stale keep-alive sockets get reaped, and bot-shaped traffic gets its connections dropped instead of a polite 429. Axios's default agent reuses sockets aggressively, which maximizes the chance of writing into a dead one.

## Edge cases / pitfalls
- `keepAlive: false` fixes the symptom at a performance cost. Tune the agent timeout instead of leaving it off forever.
- Socket hang ups that start after N requests are Cloudflare scoring the session. Slowing down is the only legitimate lever.
- Do not rotate IPs to dodge the shedding. That is evasion, and the hang ups will follow.
- Log the Cloudflare ray ID when you get an HTTP response; on hang ups there is none, which itself tells you the drop was TCP-level.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_V8HpU-pYab1_B57eoh8ViQ
