Skip to content

unexpected status 502 Bad Gateway Codex

Quick fix

Set 127.0.0.1 to direct in your proxy software, or configure NO_PROXY in the Codex environment, so the local proxy is not intercepted.

Error output
unexpected status 502 Bad Gateway: Unknown error, url: http://127.0.0.1:15721/v1/responses

cc-switch never returns a 502 of its own accord. The error happens because local proxy software (Clash, Clash Verge) intercepts requests bound for 127.0.0.1, or because the Codex reqwest library reads the system proxy environment variables and the request is hijacked into the proxy software. The proxy does not recognise the /v1/responses path and returns 502 Bad Gateway. In some cases it instead shows as a failed WebSocket upgrade reporting stream disconnected before completion.

  1. In your proxy software config (Clash, for example), force the local address to direct mode, and make sure the rule comes first.

    Clash 配置文件
    rules:
    - DOMAIN-SUFFIX,localhost,DIRECT
    - IP-CIDR,127.0.0.0/8,DIRECT,no-resolve
  2. Alternatively, add NO_PROXY to the Codex environment variable file so Codex bypasses the proxy and connects directly when reaching the local cc-switch.

    ~/.codex/.env
    HTTP_PROXY=http://127.0.0.1:7890/
    HTTPS_PROXY=http://127.0.0.1:7890/
    NO_PROXY=127.0.0.1,localhost,::1
  3. With software such as Clash Verge, try turning system proxy mode off and taking over traffic in TUN mode instead.

ToolCodex
VersionUnknown
PlatformsWindowsmacOS
Why does Claude Code work while only Codex reports 502?
The Codex Rust CLI uses the reqwest library, which reads system proxy environment variables, so requests get hijacked into the proxy software. Claude Code is unaffected by those variables and reaches cc-switch directly.
I changed the Clash rules and it still does not work. What now?
Make sure the DIRECT rules for localhost and 127.0.0.0/8 sit at the very top of the rule list (highest priority), or turn the system proxy off entirely and switch to TUN mode.

This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.