502 Bad Gateway Codex proxy takeover overwrites config.toml
Quick fix
Disable global proxy and switch to rule mode with direct connection to 127.0.0.1, or upgrade cc-switch to avoid fully overwriting config.toml.
Symptom
Section titled “Symptom”■ unexpected status 502 Bad Gateway: , url: http://127.0.0.1:15721/v1/responsesThis page covers two interrelated root causes that, in different scenarios, prevent Codex from working properly through the CC Switch local proxy.
**Root cause 1: System global proxy intercepts local loopback traffic.** When users enable the "Global Proxy" mode of proxy software such as Clash Verge / v2rayU, all traffic (including local requests sent to `127.0.0.1` and `localhost`) is forcibly routed to the proxy server. Since the CC Switch local proxy listens on `127.0.0.1:15721`, the global proxy prevents Codex from directly accessing this port, resulting in a `502 Bad Gateway`.
**Root cause 2: CC Switch proxy takeover fully overwrites config.toml.** When Codex local proxy takeover is enabled (`enableLocalProxy = true`), CC Switch uses the provider template saved in the database to perform a "full overwrite" rewrite of `~/.codex/config.toml` during startup, provider switching, or abnormal exit recovery, instead of an incremental merge. This causes user-added MCP server configurations, `[desktop]` UI preferences, `[memories]` memory settings, `[projects]` trust levels, `sandbox_mode`, and other non-provider-related personalized configuration sections to be completely discarded. Additionally, in the abnormal exit recovery path, CC Switch reads the pre-takeover full-file snapshot from the `proxy_live_backup` table and writes it back as-is, causing MCP servers deleted by the user during the takeover period to "revive".
Check the proxy mode of your proxy software (such as Clash Verge / v2rayU) and change "Global Proxy" to "Rule Proxy".
在 Clash Verge / v2rayU 规则配置中添加: # DOMAIN-SUFFIX,localhost,DIRECT# IP-CIDR,127.0.0.0/8,DIRECTEnsure that in the rule proxy, `127.0.0.1` and `localhost` use direct connection (DIRECT) and do not go through proxy nodes.
If the issue causes MCP or personalized configurations to be lost due to a CC Switch restart, upgrade CC Switch to the latest version. Enable the "Preserve Codex Official Authentication" option in settings to prevent OAuth login status from being overwritten.
If the issue persists after upgrading, as a temporary workaround, you can manually modify the `original_config` field in the `proxy_live_backup` table within the CC Switch database (`~/.cc-switch/cc-switch.db`) to append the missing TOML segments.
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 29 real issues
- #770codex开启代理后报错
- #1423codex 配置问题
- #1755貌似最新版的cc switch设置了codex CLI 配置代理后会劫持桌面codex版本的请求
- #1959默认的config.toml (TOML)格式是不全的......
- #2222[BUG] 正则表达式导致的Codex测试模型在config.toml中不在首行时会被忽略,导致回退到默认模型gpt-5.1-codex
- #3423重启电脑后 Codex 插件全部失效,需要重新安装
- #3447Windows开启路由后模型始终无法生效
- #3895Codex Desktop history temporarily disappears after provider/proxy switch
- #4012bug: stale Codex MCP servers resurrected from provider config and takeover backup
- #4254[Codex] Proxy takeover overwrites entire config.toml, silently destroying all user settings (UI preferences, MCP servers, memories, etc.)
- #4368codex重启会被cc-switch覆盖config.foml文件
- #4371功能请求:用 Codex 官方 profile(增量叠加)机制重构供应商切换,替代易出错的 config.toml 覆盖式改写
- #4423[Bug] Windows v3.16.3 — Codex (no proxy): updating provider model name does not take effect in config.toml
- #4424[Bug] Windows v3.16.3 — Codex (no proxy): updating provider model name does not take effect in config.toml
- #4969[Bug] openai_chat + routing: model catalog generated but Codex desktop doesn't display models
- #4978Mobile Codex Session bypasses CC Switch local proxy (requests not reaching router)
- #5266[回归] v3.16.5 的 NativeResponses 模型目录模板导致推理档位和图片能力退化
- #5276Bug: Save logic strips /v1 from base_url for openai_responses providers, breaking Bailian native Responses
- #5425自动 failover 到 GLM 后产生高度重复输出(gpt-5.6-luna)
- #5743[Bug] Every active Codex provider displays the same fixed API key
- #5886Codex common_config_codex and route backup can be replaced by a partial config while proxy routing is active
- #5974[Bug] 开启 Codex 本地路由后 unifyCodexSessionHistory 对 OpenAI Official 失效:官方路由硬编码 cc-switch-official 桶导致官方/第三方会话历史分裂
- #6017[Bug] 代理接管(异常退出恢复路径)重写 config.toml 时不合并「应用通用配置」,导致 sandbox_mode/web_search 等设置丢失
- #6183Codex config.toml 在启动时被自动热切换覆盖,丢失 [windows] 和 [projects] 段
- #6256[Bug] macOS 上 Codex 直连 wss://api.openai.com 绕过本地路由:需显式设置 transport_kind = responses_http
- #6265[Bug] 开启 Codex 本地代理接管后,重启 CC Switch 会清空 MCP 配置
- #6377[BUG]删除 MCP 服务器后,代理接管备份 (proxy_live_backup) 在退出时将其写回 config.toml,导致删除无效、再次导入时复活
- #6558bug(codex): CLI provider switch rewrites live config.toml from provider blob and drops permission profiles + compaction/reasoning keys
- #6860Config writer strips non-managed [model_providers.*] sections on takeover/switch, breaking old Codex threads
- What should I do if I still get the error Reconnecting... 2/5 (8s • esc to interrupt) after disabling VPN?
- This is usually caused by residual local network or proxy rules. Try replacing `127.0.0.1` with `localhost` in the configuration, and ensure the system proxy is completely disabled with no residual proxy rules intercepting local loopback traffic.
- What should I do if all Codex plugins fail after restarting the computer and require reinstallation?
- This is because CC Switch overwrote `~/.codex/auth.json` during startup, breaking the OAuth status. Please upgrade to the latest version (v3.16.4+) and enable the "Preserve Codex Official Authentication" option in settings.
- What should I do if the model in config.toml is not updated after editing the provider's model field in place?
- This is a known bug in v3.16.3, where the in-place update code path for providers missed the serialization of the `model` field. A temporary workaround is to create a new provider (or copy an existing one), set the new model, and enable it; the new flow will write the complete provider block.
- What should I do if an MCP server comes back after restarting CC Switch even though I deleted it?
- This is because the deletion operation only cleaned the `mcp_servers` table and the live configuration file, but the pre-takeover full-file snapshot in the `proxy_live_backup` table still contains that server. When CC Switch exits, the snapshot is written back to config.toml as-is, causing the server to revive. You need to clean the snapshot record in the database simultaneously.
- What should I do if Codex directly connects to wss://api.openai.com bypassing the proxy after enabling local routing on macOS?
- The `transport_kind` field of Codex defaults to `responses_websocket`, and WebSocket connections ignore `base_url` and directly connect to the official endpoint. You need to explicitly set `transport_kind = "responses_http"` in the config.toml generated by CC Switch.
Related problems
Section titled “Related problems”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.