Guide · Scraping practice
502 Bad Gateway or 503 through a proxy: did the proxy or the website send it, and should you retry?
A 502 Bad Gateway or 503 Service Unavailable through a proxy comes from one of two places, and for an HTTPS URL you can tell which. If the error names the proxy or the tunnel (curl's CONNECT tunnel failed, response 503, requests' Tunnel connection failed: 503 Service Unavailable, httpx.ProxyError), the proxy answered before any connection to the site existed. If you got an ordinary response with status 502 or 503, the website or its CDN sent it through the tunnel, because the proxy cannot write into the encrypted stream. Node is the exception: https-proxy-agent, which axios uses, replays the proxy's refusal as a normal response, so Request failed with status code 503 can be either. Both codes are worth a few retries with backoff on a new connection. If the same 502 comes back on every attempt for one hostname, stop and check the hostname and port.
Reviewed 14 Nov 2026
The short answer
- For HTTPS URLs: an exception that mentions the proxy or the tunnel is the proxy's answer; a response object with status 502 or 503 is the site's. Reproduced with curl, requests, httpx and aiohttp.
- curl shows both numbers:
-w '%{http_connect} %{http_code}'printed503 000when the proxy refused the tunnel and200 502when the site failed behind a working tunnel. - In Node, axios 1.21.1 and got 16.2.0 (via https-proxy-agent) reported a proxy's CONNECT 503 with the same text as a site's 503. undici names the proxy:
Proxy response (503) !== 200 when HTTP Tunneling. - urllib3's
RetryhonoursRetry-Afteronly on responses. The same503withRetry-After: 5meant 15 seconds of waiting as a response and none at all as an answer to CONNECT. - Plain
http://URLs have no tunnel, so a proxy's 503 arrives as a normal response. Compare headers such asServerto see who sent it.
What 502 and 503 mean
RFC 9110 defines 502 Bad Gateway as a server "acting as a gateway or proxy" that "received an invalid response from an inbound server", and 503 Service Unavailable as a server that is temporarily unable to handle the request, for example from overload or maintenance, which "MAY send a Retry-After header field". Any hop can send either one: the proxy, a CDN in front of the site, or the site's own load balancer.
What a particular proxy puts behind each code is its own choice. Our local test proxy answered CONNECT with 502 when the target hostname did not resolve, a second test proxy answered everything with 503 and Retry-After: 5, and a local HTTPS site returned 502 and 503 itself. The tables below come from those three.
Who sent it: the proxy or the site
Reproduced on 2026-11-14. The middle column is the proxy refusing CONNECT for https://example.com/; the right column is a 502 from the HTTPS site behind a working tunnel.
| Client (version run) | Proxy refused the tunnel | Site answered 502 through the tunnel |
|---|---|---|
curl 8.7.1, -w '%{http_connect} %{http_code}' | curl: (56) CONNECT tunnel failed, response 503, exit 56, 503 000 | Exit 0, 200 502 |
| Python requests 2.34.2 | ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')) | No exception, r.status_code == 502 |
| httpx 0.28.1 | httpx.ProxyError: 503 Service Unavailable | No exception, status 502 |
| aiohttp 3.14.5 | ClientHttpProxyError: 503, message='Service Unavailable', url='http://127.0.0.1:28084' | No exception, status 502 |
axios 1.21.1, proxy option or https-proxy-agent 9.1.0 | AxiosError: Request failed with status code 503, err.response.status 503 | AxiosError: Request failed with status code 502: the same form |
| got 16.2.0 + https-proxy-agent 9.1.0 | HTTPError: Request failed with status code 503 (Service Unavailable): GET https://example.com/ | HTTPError: Request failed with status code 502 (Bad Gateway): GET https://... |
undici 7.30.2 fetch + ProxyAgent | TypeError: fetch failed, cause Proxy response (503) !== 200 when HTTP Tunneling | No exception, res.status === 502 |
| Chrome 157 and Chromium 156 | net::ERR_TUNNEL_CONNECTION_FAILED, see that guide | Not tested here |
Why the Node rows look alike: when the proxy answers CONNECT with anything but 200, https-proxy-agent closes the proxy socket and replays the proxy's buffered answer onto a fake socket "so that the node core http can parse and handle the error status code", as its source comment says. Your code then sees the proxy's 503 as a response for the site's URL. To tell them apart, repeat the request with curl and read %{http_connect}, or use undici, which reports the proxy's status separately. requests, curl and the other Python clients are covered in more detail in requests ProxyError and CONNECT tunnel failed.
Plain http:// URLs: no tunnel, so compare the headers
For an http:// URL the client sends the whole request to the proxy, and the proxy's 502 or 503 comes back as the response itself. curl exited 0 with %{http_connect} = 000 and %{http_code} = 503; requests, httpx and aiohttp returned status 503 without an exception. The response is the only clue: our test site sent Server: test-origin, while the proxy's 503 carried just Retry-After, Content-Length and Connection. Compare the headers and body with a request that works.
If the site offers it, request the https:// URL instead: the tunnel separates the proxy's answers from the site's.
Retrying 502 and 503 without making it worse
urllib3's Retry honours Retry-After by default for 413, 429 and 503, but only on responses. A refused CONNECT reaches it as an exception, so the header is never read. Measured with Retry(total=3, status_forcelist=[502, 503]) against our 503 proxy:
| Setup | Requests the proxy received | Time | Result |
|---|---|---|---|
https:// URL, proxy refuses CONNECT, backoff_factor=0 | 4 | 0.0 s: Retry-After ignored | ProxyError ... Tunnel connection failed: 503 Service Unavailable |
http:// URL, proxy's 503 is the response, backoff_factor=0 | 4 | 15.0 s: waited 5 s three times | RetryError ... (Caused by ResponseError('too many 503 error responses')) |
https:// URL, proxy refuses CONNECT, backoff_factor=1 | 4 | 6.0 s: 0 + 2 + 4 s of backoff | ProxyError |
So for tunnel failures, backoff is all you get: set backoff_factor, or a proxy that asks you to wait gets the retries at once. urllib3 sleeps backoff_factor * 2 ** (n - 1) seconds and skips the sleep before the first retry, which matches the 0 + 2 + 4 s we measured.
A retry needs a new connection to reach a different exit. With a test proxy that failed every other connection, urllib3 retried the 502 on a new connection and got 200; the next two calls reused the open tunnel and sent no new CONNECT. On a rotating proxy rotation happens per connection, and with a sticky session a new _session- handle draws a new exit (rotating vs sticky sessions).
Cap it at three attempts with backoff, then log and move on. A 502 that returns on every attempt for one hostname is not load: our proxy answered 502 to a misspelled hostname every time. The 429 guide adds jitter and per-host limits.
Code
Worked example
curl -sS -o /dev/null \ -w 'proxy answered CONNECT with %{http_connect}, site answered %{http_code}\n' \ -x "http://USERNAME_pool-arcfast_country-de:PASSWORD@proxy.arcproxies.io:8000" https://example.com/# Reproduced locally on 2026-11-14, curl 8.7.1:# proxy refused: curl: (56) CONNECT tunnel failed, response 503# proxy answered CONNECT with 503, site answered 000# site failed: proxy answered CONNECT with 200, site answered 502import requestsfrom requests.adapters import HTTPAdapterfrom urllib3.util import RetryPROXY = "http://USERNAME_pool-arcfast_country-de:PASSWORD@proxy.arcproxies.io:8000"session = requests.Session()retry = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504], allowed_methods=["GET", "HEAD"])session.mount("http://", HTTPAdapter(max_retries=retry))session.mount("https://", HTTPAdapter(max_retries=retry))try: r = session.get("https://example.com/", proxies={"http": PROXY, "https": PROXY}, timeout=30) print("status", r.status_code)except requests.exceptions.ProxyError as err: print("the proxy refused the tunnel:", err) # e.g. Tunnel connection failed: 503 Service Unavailableexcept requests.exceptions.RetryError as err: print("the site kept answering 5xx:", err) # came through the tunnel, not from the proxy# Ran locally: ProxyError after 6.0 s against a proxy answering 503,# RetryError ('too many 502 error responses') after 6.1 s against a site answering 502,# status 200 through a proxy that failed the first connection only.FAQ
Questions
Something not covered? Write to contact@arcproxies.io.
Does a 502 through a proxy mean the proxy is down?
Not necessarily. A 502 means some gateway got an invalid response from the server behind it. If your client reports it as a tunnel or proxy error, the proxy sent it, often because it could not reach the target. If it arrives as a normal response to an https:// URL, the website or its CDN sent it.
How do I know if a 503 came from the proxy or the website?
For https:// URLs, run curl with -w '%{http_connect} %{http_code}'. 503 000 means the proxy refused the tunnel; 200 503 means the site answered 503 through it. In Python, a ProxyError is the proxy and a response with status 503 is the site.
Why does axios say Request failed with status code 503 when the site works?
With a proxy, axios tunnels through https-proxy-agent, which replays the proxy's refusal of the CONNECT request as an ordinary response. The 503 may come from the proxy. Check the same URL with curl and %{http_connect}, or use undici, which reports Proxy response (503).
Should I retry 502 and 503 errors through a proxy?
Yes, a few times: up to three attempts with exponential backoff, on a new connection so a rotating proxy can pick another exit. Stop if the same error returns every time for one hostname, and honour Retry-After when the site sends it.
Does requests honour Retry-After from the proxy?
Only when the proxy's 503 is a response, which happens for http:// URLs. When the proxy refuses the CONNECT for an https:// URL, urllib3 treats it as a connection error and retried immediately in our test, so set backoff_factor.
Sources & review
Last reviewed by the ARC Proxies team. Prices are read from the live catalog at build time.
- [1]IETF: RFC 9110: HTTP Semantics, 15.6.3 502 Bad Gateway
- [2]IETF: RFC 9110: HTTP Semantics, 15.6.4 503 Service Unavailable
- [3]IETF: RFC 9110: HTTP Semantics, 10.2.3 Retry-After
- [4]IETF: RFC 9110: HTTP Semantics, 9.3.6 CONNECT
- [5]urllib3 2.8.0: src/urllib3/util/retry.py (RETRY_AFTER_STATUS_CODES, get_backoff_time)
- [6]curl: curl man page (--proxy, --proxy-user, -w http_connect)
- [7]https-proxy-agent 9.1.0: dist/index.js (non-200 CONNECT answers replayed onto a fake socket)
- [8]undici 7.30.2: lib/dispatcher/proxy-agent.js ("Proxy response (...) !== 200 when HTTP Tunneling")
Next
Where to go from here
Guides