Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python requests gets a 403 from the proxy before it reaches the site

Your error

requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /v1/items (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 403 Forbidden')))1 more line it can show

Fix it fast

Most common cause, 1 of 3

requests uses a proxy from the environment that you did not choose

If the host should be reached directly, list it in NO_PROXY or stop the session from reading the environment.

Fix
import requests

s = requests.Session()
s.trust_env = False  # ignore HTTP(S)_PROXY and friends
r = s.get("https://api.example.com/v1/items", timeout=10)

# or keep the env proxies but skip one host:
# export NO_PROXY="api.example.com"
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ProxyError with Tunnel connection failed: 403 Forbidden means the proxy, not the website, refused the HTTPS tunnel. Either requests took an unwanted proxy from HTTPS_PROXY, or the proxy's rules deny your client address or the target port. Exclude the host with NO_PROXY or fix the proxy's access rules. Applies to requests 2.34 and urllib3 2.8.

  • Applies to requests 2.34.2, urllib3 2.8.0, Python 3.13 to 3.15, Squid default squid.conf rules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the proxies Python reads from the environment, then repeat the request with curl through the same proxy and watch the reply to CONNECT. An entry you did not expect points to cause 1; a 403 to CONNECT on port 443 from your machine points to cause 2; a 403 only for another port points to cause 3.

Run this first
python -c "import urllib.request; print(urllib.request.getproxies())"
curl -v --proxy http://10.10.1.10:3128 https://api.example.com/

Then find the line you see in this list and open its fix:

  1. 1requests uses a proxy from the environment that you did not chooseYour code passes no proxies argument, yet getproxies() prints an https entry from HTTPS_PROXY or ALL_PROXY
  2. 2The proxy's access rules do not allow your clientcurl through the proxy also gets 403 to CONNECT for any HTTPS site on port 443, while the same command run on the proxy host works
  3. 3The target port is not on the proxy's list of TLS portsRequests to port 443 pass the proxy, but a URL such as https://api.example.com:8443/ fails with 403

What does the error look like?

  • requests.exceptions.ProxyError: HTTPSConnectionPool(host='api.example.com', port=443): Max retries exceeded with url: /v1/items (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 403 Forbidden')))
  • OSError: Tunnel connection failed: 403 Forbidden

Cause 1 of 3

requests uses a proxy from the environment that you did not choose

You seeYour code passes no proxies argument, yet getproxies() prints an https entry from HTTPS_PROXY or ALL_PROXY

Check

When a request does not set proxies itself, requests takes the proxy from the http_proxy, https_proxy, all_proxy and no_proxy variables, upper or lower case. A corporate shell profile, a CI image or a container can set them, and that proxy then refuses the destination with 403. A dict in session.proxies does not win, because environment proxies overwrite it.

Check
env | grep -i _proxy

Fix

If the host should be reached directly, list it in NO_PROXY or stop the session from reading the environment. If a different proxy is the right one, pass it in the proxies argument of each request, which the requests docs name as the way to beat environment settings.

Fix
import requests

s = requests.Session()
s.trust_env = False  # ignore HTTP(S)_PROXY and friends
r = s.get("https://api.example.com/v1/items", timeout=10)

# or keep the env proxies but skip one host:
# export NO_PROXY="api.example.com"

Verify

The request returns the site's own status code, and the ProxyError is gone.

Verify
python -c "import requests; s=requests.Session(); s.trust_env=False; print(s.get('https://api.example.com/', timeout=10).status_code)"

Confirmed by requests.readthedocs.io

Did this fix it?

Cause 2 of 3

The proxy's access rules do not allow your client

You seecurl through the proxy also gets 403 to CONNECT for any HTTPS site on port 443, while the same command run on the proxy host works

Check

Squid's suggested squid.conf only lets localhost through and ends with http_access deny all; the allow localnet line ships commented out. Every refused request receives 403 Forbidden unless deny_info sets another code, so a client outside the allowed networks sees exactly this tunnel failure.

Check
grep -nE "^(acl|http_access)" squid.conf

Fix

If you run the proxy, define an acl for your client network and add an http_access allow line for it above deny all, then reload Squid. If someone else runs it, send them your public IP and the target host; there is nothing to change in the Python code.

Fix
# squid.conf, above "http_access deny all"
acl office src 203.0.113.0/24
http_access allow office

Verify

curl -v through the proxy no longer shows a 403 reply to CONNECT, and the Python request succeeds.

Verify
curl -v --proxy http://10.10.1.10:3128 https://api.example.com/

Confirmed by squid-cache.org

Did this fix it?

Cause 3 of 3

The target port is not on the proxy's list of TLS ports

You seeRequests to port 443 pass the proxy, but a URL such as https://api.example.com:8443/ fails with 403

Check

The default Squid rules include http_access deny CONNECT !SSL_ports, and the stock SSL_ports acl lists only port 443. A tunnel to any other port is refused before the target server is contacted.

Check
grep -n "SSL_ports" squid.conf

Fix

Add the port to SSL_ports on a proxy you control, or ask its owner to allow it. Serving the API on 443 avoids the problem for every client behind such proxies.

Fix
# squid.conf
acl SSL_ports port 443
acl SSL_ports port 8443

Verify

curl -v to the 8443 URL gets past the proxy, and requests returns the API's response.

Verify
curl -v --proxy http://10.10.1.10:3128 https://api.example.com:8443/

Confirmed by squid-cache.org

Did this fix it?

How do I stop it coming back?

  • Log urllib.request.getproxies() once at service start-up, so a proxy inherited from the host shows up in the logs.
  • Set NO_PROXY for internal and partner API hosts in the same place where HTTPS_PROXY is set for the machine or container.
  • When a service must use a proxy, pass proxies per request from config instead of relying on whatever the environment holds.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.