Skip to content

fix(http): preserve user-supplied Host header when forwarding through a proxy (#10805)#10822

Merged
jasonsaayman merged 4 commits intoaxios:v1.xfrom
ashishkr96:fix/10805-preserve-host-header-with-proxy
Apr 30, 2026
Merged

fix(http): preserve user-supplied Host header when forwarding through a proxy (#10805)#10822
jasonsaayman merged 4 commits intoaxios:v1.xfrom
ashishkr96:fix/10805-preserve-host-header-with-proxy

Conversation

@ashishkr96
Copy link
Copy Markdown
Contributor

@ashishkr96 ashishkr96 commented Apr 29, 2026

Summary

Fixes #10805.

When sending a request through a proxy with the http adapter, axios was unconditionally rewriting options.headers.host to the request URL's hostname:port, overwriting any Host header the caller had set in config.headers. That made it impossible to direct a proxy to a virtual host that differs from the request URL.

Before

axios.get("http://127.0.0.1:4000", {
  headers: { host: "example.com" },
  proxy: { protocol: "http", host: "127.0.0.1", port: 8888 },
});

The proxy received:

GET / HTTP/1.1
host: 127.0.0.1:4000

After

The proxy receives:

GET / HTTP/1.1
host: example.com

Change

In lib/adapters/http.js#setProxy, only assign the default Host header when the caller has not already provided one. The check is case-insensitive so users can pass host, Host, or HOST.

Test plan

  • New unit tests in tests/unit/adapters/http.test.js covering the default behavior, lowercase user header, and mixed-case user header (Host header preservation when forwarding through a proxy (#10805) describe block).
  • All existing proxy-related unit tests still pass (vitest run --project unit -t "proxy" — 46 passed).
  • End-to-end smoke check with a real HTTP proxy + target server confirms the proxy now sees the user-supplied Host value while the target still sees its own hostname.

Summary by cubic

Preserves a user-supplied Host header when requests go through a proxy in the Node http adapter for axios. This lets callers route to virtual hosts via proxies without the header being overwritten.

Description

  • Summary of changes: Skip setting options.headers.host if the user already set any Host/host header (case-insensitive). Otherwise default to hostname:port.
  • Reasoning: Needed to target virtual hosts through proxies and avoid clobbering caller intent.
  • Additional context: Behavior is limited to proxy forwarding logic; non-proxy requests are unchanged.

Docs

Please update /docs/ to note that axios respects a user-supplied Host header when using a proxy, with a short example showing virtual host routing via proxies.

Testing

  • Added unit tests for:
    • Defaulting when no Host is provided.
    • Preservation with lowercase host and mixed-case Host.
  • All existing proxy tests still pass.
  • Manual smoke check with a real HTTP proxy confirms the proxy sees the user-supplied Host.

Semantic version impact

Patch (bug fix, no API changes).

Written for commit 584f284. Summary will update on new commits. Review in cubic

… a proxy (axios#10805)

When sending a request through a proxy with the http adapter, axios was
unconditionally rewriting `options.headers.host` to the request URL's
hostname:port, overwriting any Host header the caller had set in
`config.headers`.

This made it impossible to direct a proxy to a virtual host that differs
from the request URL — for example, hitting `127.0.0.1:4000` while
asking the proxy to treat the request as `example.com`.

Skip the default assignment when a Host header is already present
(case-insensitive match, since users may pass `host`, `Host`, or `HOST`).
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

@ashishkr96
Copy link
Copy Markdown
Contributor Author

@jasonsaayman can you pls review this

@jasonsaayman jasonsaayman added priority::medium A medium priority commit::fix The PR is related to a bugfix labels Apr 30, 2026
@jasonsaayman jasonsaayman merged commit 0e8b6bb into axios:v1.x Apr 30, 2026
29 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commit::fix The PR is related to a bugfix priority::medium A medium priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

If user set a customized header host field, the host should not be changed while sending request.

2 participants