A proxy error 502 Bad Gateway means a server acting as a gateway tried to get a response from the next server in the chain and got nothing usable back. When you're using a proxy, there are at least three hops: your client to the proxy, the proxy to an exit IP, and the exit IP to the website. A 502 tells you one of the later hops failed. It almost never means your username or password is wrong, because authentication problems produce a 407 instead.
This guide shows how to work out which hop produced the 502, what usually causes it, and what to change in your code so a few 502s don't break a whole job.
Who Sent the 502? Proxy vs. Website
The first job is figuring out whether the 502 came from the proxy or from the website behind it. They look identical in a status code but mean different things.
- A 502 returned in response to the CONNECT request (before any TLS handshake) comes from the proxy. It means the proxy authenticated you but couldn't open a tunnel to the target through the exit it picked.
- A 502 returned after the TLS handshake, inside the tunnel, comes from the website or its CDN. The proxy did its job; the site's own load balancer couldn't reach its backend.
- A 502 on a plain http:// URL can be either. Look at the response body and headers: a CDN-branded error page or a Server header naming the site's stack points to the website.
curl's verbose output makes this obvious. Run the request with -v and read where the 502 appears:
curl -v -x http://USERNAME:[email protected]:8080 https://example.com -o /dev/null
# Proxy-side failure looks like:
# > CONNECT example.com:443 HTTP/1.1
# < HTTP/1.1 502 Bad Gateway
# curl: (56) CONNECT tunnel failed, response 502
# Website-side failure looks like:
# < HTTP/1.1 200 Connection established
# * SSL connection using TLSv1.3 ...
# < HTTP/2 502If you see "200 Connection established" followed by a TLS handshake and then a 502, the proxy is working and the site is having trouble. Proxies can't fix that; retrying later or slowing down is the answer.
Common Causes of a Proxy 502 Bad Gateway
The exit IP couldn't reach the target
Residential and mobile exits are real devices on home and carrier networks. Occasionally one drops offline mid-request, or its network can't reach a particular host. On the NodeProxies gateway, a 502 on CONNECT usually means the upstream or target failed after your credentials were already accepted. With rotating sessions, the next request goes through a different exit, so a simple retry fixes most of these.
The target host is down or refusing connections
If the website itself is down, every exit will fail and every retry will return a 502. Check the site directly from your own connection (no proxy) or from a second network. If it fails there too, the problem isn't the proxy.
Wrong hostname or port in the URL
A typo in the target hostname, or a non-standard port the exit can't reach, shows up as a gateway error because the proxy can't open the tunnel. Double-check the URL, especially when it's built from variables.
Sticky session pinned to a dead exit
If you're using a sticky session (USERNAME-session-abc123-time-10), every request with that session id goes through the same exit until the time runs out. If that exit has gone offline, you'll get repeated 502s. Switch to a new session id and you get a new IP. See rotating vs. sticky proxies for how sessions behave.
How to Fix a 502 Through a Proxy
- Confirm the proxy itself works with a neutral endpoint: curl -x http://USERNAME:[email protected]:8080 https://api.ipify.org. If that returns an IP, your setup is fine. Our guide on testing whether a proxy is working has more checks.
- Retry the failing request with a fresh IP. Without a session parameter you already get a new IP per request. With a sticky session, generate a new session id for the retry.
- Check the target directly. If it fails without the proxy too, wait or contact the site owner.
- Try a different country. If one country's exits consistently can't reach a host, add -country-us or another code to the username and compare.
- Lower your concurrency. Some targets return 502s from their own load balancers when they're overloaded, and hammering them makes it worse.
Retrying 502 Errors Automatically
Because most proxy-side 502s are transient, the right production fix is a small retry loop with backoff. In Python with requests, urllib3's Retry handles it:
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
PROXY = "http://USERNAME:[email protected]:8080"
retry = Retry(
total=4,
backoff_factor=1, # waits 0s, 2s, 4s, 8s between attempts
status_forcelist=[502, 503, 504],
allowed_methods=["GET", "HEAD"],
)
session = requests.Session()
session.mount("http://", HTTPAdapter(max_retries=retry))
session.mount("https://", HTTPAdapter(max_retries=retry))
session.proxies = {"http": PROXY, "https": PROXY}
r = session.get("https://example.com", timeout=30)
print(r.status_code)Note that a 502 on CONNECT is raised by requests as a ProxyError rather than a status code, and urllib3 still retries it within the same total budget. If four fresh IPs in a row all get a 502, the target is the problem.
502 vs. Other Proxy Error Codes
- 407 Proxy Authentication Required: wrong credentials or the plan isn't active. See how to fix proxy error 407.
- 402: the plan is out of bandwidth. Top up and the same credentials work again.
- 502 Bad Gateway: auth passed, the next hop failed. Retry with a new IP.
- 503 Service Unavailable: a server is temporarily unable to handle the request. See proxy error 503.
- 504 Gateway Timeout: the next hop was reached but didn't answer in time. See proxy timeout errors.
Handling 502s with NodeProxies
With rotating residential proxies, every request without a session parameter goes out through a different IP, so the retry loop above is usually all you need. Failed CONNECT attempts don't transfer page content, so they cost very little bandwidth. If you're starting fresh, create an account, add balance from $3, and generate credentials in the dashboard. Pricing is per GB on the pricing page, with no subscription.
FAQ
Does a 502 Bad Gateway mean my proxy is blocked?
Not usually. A block typically comes back as a 403, a 429 or a CAPTCHA page from the website. A 502 means a gateway couldn't get a valid response from the next server, which is more often a connectivity problem than a block.
Is a 502 my fault or the proxy provider's?
Check with curl -v. If the 502 comes back on the CONNECT line, the proxy couldn't reach the target through that exit. If it comes after the TLS handshake, the website sent it. In neither case are your credentials the issue.
How many times should I retry a 502?
Two to four retries with exponential backoff is a sensible default. If several fresh IPs all fail, stop and check whether the target is down.
Why do I get 502 only on one website?
That points to the website: it may be down, overloaded, or refusing connections from certain networks. Test it without a proxy and from a different proxy country to narrow it down.
