Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/fix-proxy-localhost.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": patch
---

Stop proxying localhost requests when proxy environment variables are set

When `HTTP_PROXY` or `HTTPS_PROXY` is configured, all fetch requests including ones to `localhost` were routed through the proxy. This caused `wrangler dev` and the Vite plugin to fail with "TypeError: fetch failed" because the proxy can't reach local addresses.

This switches from `ProxyAgent` to undici's `EnvHttpProxyAgent`, which supports the `NO_PROXY` environment variable. When `NO_PROXY` is not set, it defaults to `localhost,127.0.0.1,::1` so local requests are never proxied.

The `NO_PROXY` config only applies to the request destination, not the proxy server address. So a proxy running on localhost (e.g. HTTP_PROXY=http://127.0.0.1:11451) still works for outbound API calls.
8 changes: 5 additions & 3 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
experimental_readRawConfig,
} from "@cloudflare/workers-utils";
import chalk from "chalk";
import { ProxyAgent, setGlobalDispatcher } from "undici";
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
import makeCLI from "yargs";
import { version as wranglerVersion } from "../package.json";
import { aiFineTuneNamespace, aiNamespace } from "./ai";
Expand Down Expand Up @@ -337,7 +337,7 @@ import {
logoutCommand,
whoamiCommand,
} from "./user/commands";
import { proxy } from "./utils/constants";
import { noProxy, proxy } from "./utils/constants";
import { debugLogFilepath } from "./utils/log-file";
import { vectorizeCreateCommand } from "./vectorize/create";
import { vectorizeCreateMetadataIndexCommand } from "./vectorize/createMetadataIndex";
Expand Down Expand Up @@ -394,7 +394,9 @@ import type { LoggerLevel } from "./logger";
import type { CommonYargsArgv, SubHelp } from "./yargs-types";

if (proxy) {
setGlobalDispatcher(new ProxyAgent(proxy));
setGlobalDispatcher(
new EnvHttpProxyAgent({ noProxy: noProxy || "localhost,127.0.0.1,::1" })
);
logger.log(
`Proxy environment variables detected. We'll use your proxy for fetch requests.`
);
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const proxy =
process.env.http_proxy ||
process.env.HTTP_PROXY ||
undefined;
export const noProxy =
process.env.no_proxy || process.env.NO_PROXY || undefined;
2 changes: 2 additions & 0 deletions packages/wrangler/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"HTTP_PROXY",
"https_proxy",
"HTTPS_PROXY",
"no_proxy",
"NO_PROXY",
"HYPERDRIVE_DATABASE_URL",
"HYPERDRIVE_MYSQL_DATABASE_URL",
"WRANGLER_DOCKER_BIN",
Expand Down
Loading