help request: Unable to remove or override `X-Forwarded-Host` header per-route using any plugin
Description
I need to remove (or clear) the X-Forwarded-Host header for a specific API route before it reaches the upstream service. Despite trying every available approach, APISIX always sends the original Host header value as X-Forwarded-Host to the upstream.
What I've Tried
1. proxy-rewrite with headers.remove
{
"plugins": {
"proxy-rewrite": {
"headers": {
"remove": ["X-Forwarded-Host"]
}
}
}
}
Result: X-Forwarded-Host still sent to upstream with original value.
2. proxy-rewrite with headers.set (empty string)
{
"plugins": {
"proxy-rewrite": {
"headers": {
"set": {
"X-Forwarded-Host": ""
}
}
}
}
}
Result: X-Forwarded-Host still sent to upstream with original value. Not overridden to empty string.
3. proxy-rewrite with host field
{
"plugins": {
"proxy-rewrite": {
"host": "my-upstream.example.com"
}
}
}
Result: X-Forwarded-Host unchanged.
4. Upstream pass_host: rewrite with upstream_host
{
"upstream": {
"pass_host": "rewrite",
"upstream_host": "my-upstream.example.com",
"nodes": { "my-upstream.example.com:443": 1 },
"scheme": "https"
}
}
Result: X-Forwarded-Host still sent with the APISIX ingress hostname.
5. serverless-pre-function setting ngx.var.var_x_forwarded_host
{
"plugins": {
"serverless-pre-function": {
"phase": "before_proxy",
"functions": [
"return function(conf, ctx) ngx.var.var_x_forwarded_host = '' end"
]
}
}
}
Result: Plugin accepted (no errors), but X-Forwarded-Host still sent with original value.
6. Custom plugin in before_proxy phase
Created a custom Lua plugin that runs in the before_proxy phase and sets ngx.var.var_x_forwarded_host = ''.
Result: Plugin accepted, but X-Forwarded-Host still sent with original value.
Expected Behavior
At least one of the following should work:
proxy-rewritewithheaders.remove: ["X-Forwarded-Host"]should prevent the header from being sent upstreamproxy-rewritewithheaders.set: { "X-Forwarded-Host": "" }should override the header valueserverless-pre-functionwithngx.var.var_x_forwarded_host = ''inbefore_proxyphase should clear the Nginx variable beforeproxy_set_headeruses it- A custom plugin in
before_proxyphase should be able to modifyvar_x_forwarded_host
Actual Behavior
None of the above approaches have any effect on the X-Forwarded-Host header. The upstream always receives X-Forwarded-Host set to the APISIX ingress hostname.
Environment
APISIX Version: 3.17.0 Deployment: Standalone with APISIX Ingress Controller OS: Linux (K8s pods)
Reproduced on released 3.17.0 and on current master. This is a bug, not intended behavior, and the core of it is a regression introduced in 3.14.0 by #12551. There are two distinct defects; both are described below with a self-contained reproduction.
whenever the client is not covered by apisix.trusted_addresses — which is the default, since trusted_addresses is unset out of the box — every plugin-level attempt to rewrite X-Forwarded-Host is silently discarded, including the before_proxy / ngx.var.var_x_forwarded_host escape hatch. Separately, there has never been a way to delete the header on the way upstream. Setting apisix.trusted_addresses to cover your ingress network is an effective workaround for the override case today.
Reproduction
Standalone APISIX plus an nginx echo upstream. No etcd and no ingress controller needed.
echo.conf:
server {
listen 80;
location / {
default_type text/plain;
return 200 "path=$uri host=[$http_host] xfh=[$http_x_forwarded_host]\n";
}
}
config.yaml:
apisix:
node_listen: 9080
enable_admin: false
deployment:
role: data_plane
role_data_plane:
config_provider: yaml
apisix.yaml:
routes:
- id: baseline
uri: /baseline
upstream_id: echo
- id: remove
uri: /remove
upstream_id: echo
plugins:
proxy-rewrite:
headers:
remove:
- "X-Forwarded-Host"
- id: set-empty
uri: /set-empty
upstream_id: echo
plugins:
proxy-rewrite:
headers:
set:
X-Forwarded-Host: ""
- id: set-value
uri: /set-value
upstream_id: echo
plugins:
proxy-rewrite:
headers:
set:
X-Forwarded-Host: "my-upstream.example.com"
- id: before-proxy
uri: /before-proxy
upstream_id: echo
plugins:
serverless-pre-function:
phase: before_proxy
functions:
- "return function(conf, ctx) ngx.var.var_x_forwarded_host = '' end"
upstreams:
- id: echo
type: roundrobin
nodes:
"echo:80": 1
#END
docker-compose.yml:
services:
apisix:
image: apache/apisix:3.17.0-debian
volumes:
- ./config.yaml:/usr/local/apisix/conf/config.yaml:ro
- ./apisix.yaml:/usr/local/apisix/conf/apisix.yaml:ro
ports:
- "9080:9080"
depends_on: [echo]
echo:
image: nginx:alpine
volumes:
- ./echo.conf:/etc/nginx/conf.d/default.conf:ro
$ docker compose up -d
$ for p in baseline remove set-empty set-value before-proxy; do
printf '%-14s ' "$p"; curl -s -H 'Host: apisix.example.com' http://127.0.0.1:9080/$p
done
baseline path=/baseline host=[apisix.example.com] xfh=[apisix.example.com]
remove path=/remove host=[apisix.example.com] xfh=[apisix.example.com]
set-empty path=/set-empty host=[apisix.example.com] xfh=[apisix.example.com]
set-value path=/set-value host=[apisix.example.com] xfh=[apisix.example.com]
before-proxy path=/before-proxy host=[apisix.example.com] xfh=[apisix.example.com]
Every approach from the original report fails, including the before_proxy one — that part of the report is accurate.
Now add the client network to trusted_addresses in config.yaml and restart, changing nothing else:
apisix:
node_listen: 9080
enable_admin: false
trusted_addresses:
- "172.16.0.0/12" # the docker bridge the request arrives from
baseline path=/baseline host=[apisix.example.com] xfh=[apisix.example.com]
remove path=/remove host=[apisix.example.com] xfh=[apisix.example.com]
set-empty path=/set-empty host=[apisix.example.com] xfh=[apisix.example.com]
set-value path=/set-value host=[apisix.example.com] xfh=[my-upstream.example.com] <-- now works
before-proxy path=/before-proxy host=[apisix.example.com] xfh=[] <-- now works
Two behaviors flip purely on whether the peer is trusted, which is what pins the root cause. remove and set-empty stay broken either way — that is the second, independent defect.
| Route | Expected | 3.17.0, untrusted (default) | 3.17.0, trusted |
|---|---|---|---|
remove |
header absent upstream | apisix.example.com ❌ |
apisix.example.com ❌ |
set-empty |
header absent upstream | apisix.example.com ❌ |
apisix.example.com ❌ |
set-value |
my-upstream.example.com |
apisix.example.com ❌ |
my-upstream.example.com ✅ |
before-proxy |
header absent upstream | apisix.example.com ❌ |
absent ✅ |
Defect 1 — plugin rewrites of X-Forwarded-* are silently discarded for untrusted clients (regression, 3.14.0+)
handle_x_forwarded_headers() (apisix/init.lua:689 on master) runs before route matching, and for an untrusted peer it writes the APISIX-observed value into the ctx.var cache at apisix/init.lua:736:
api_ctx.var.http_x_forwarded_host = host
A plugin that later rewrites the header goes through core.request.set_header(), which tries to invalidate that cache entry at apisix/core/request.lua:155-156:
-- when the header is updated, clear cache of ctx.var
ctx.var["http_" .. str_lower(header_name)] = nil
For X-Forwarded-Host this computes the key http_x-forwarded-host (hyphens), while the ctx.var cache is keyed http_x_forwarded_host (underscores). The invalidation never matches. The stale value survives, and set_upstream_x_forwarded_headers() (apisix/init.lua:768) copies it into $var_x_forwarded_host, which is what proxy_set_header X-Forwarded-Host $var_x_forwarded_host (apisix/cli/ngx_tpl.lua:883) sends upstream.
Instrumenting a before_proxy function on a route that also has proxy-rewrite setting the header shows the two views diverging:
DIAG ctxvar_http=apisix.example.com ngxvar_http=my-upstream.example.com var_x_forwarded_host=apisix.example.com
ngx.var holds the plugin's value, the ctx.var cache still holds the pre-plugin one, and the cached one wins. When the peer is trusted, handle_x_forwarded_headers() returns early, nothing is ever cached, the read falls through to the live ngx.var, and the same config works — which is exactly the flip shown above.
The mis-keyed invalidation is not specific to X-Forwarded-*: any hyphenated header read back through ctx.var.http_* after core.request.set_header() returns a stale value. It only became load-bearing here because #12551 started pre-populating that particular cache entry.
This is a regression, and APISIX's own tests say so
Rewriting these headers via proxy-rewrite is intended, tested behavior: PR #8200 ("fix: can not modify x-forwarded-host in proxy rewrite", merged 2022-11-03) added the propagation specifically to support it, and t/plugin/proxy-rewrite3.t TEST 12/13 locks it in.
Those tests still pass only because the test harness trusts the client. t/APISIX.pm:106-107 sets:
trusted_addresses:
- "127.0.0.1"
so every test request takes the trusted branch and the cache is never populated. Delete those two lines from t/APISIX.pm and re-run the unmodified suite on master:
$ prove -Itest-nginx/lib -I. t/plugin/proxy-rewrite3.t
# Failed test 't/plugin/proxy-rewrite3.t TEST 13: rewrite X-Forwarded-Host - header X-Forwarded-Host ok'
# got: 'localhost'
# expected: 'test.com'
# Failed test 't/plugin/proxy-rewrite3.t TEST 34: test if X-Forwarded-Port can be set before proxy - header X-Forwarded-Port ok'
# got: '1984'
# expected: '9882'
Result: FAIL
Two existing regression tests, written to guarantee exactly this capability, fail the moment the harness stops trusting the client. The behavior contradicts the project's own encoded intent, and X-Forwarded-Port is affected identically to X-Forwarded-Host. That makes this a bug rather than a design decision — and it is also why CI has not caught it since 3.14.0.
Note on master vs 3.17.0 for the before_proxy escape hatch
On current master the before_proxy route above does work, because #13611 (merged 2026-07-01, after the 3.17.0 release) moved set_upstream_x_forwarded_headers() to run before handle_upstream(), so a before_proxy plugin now gets the last write to $var_x_forwarded_host. On 3.17.0 the order is the reverse (handle_upstream at init.lua:870, set_upstream_x_forwarded_headers at init.lua:872), so the stale cached value overwrites whatever the plugin set. The underlying stale-cache defect is unchanged on master — proxy-rewrite still cannot override the header for an untrusted peer there:
$ prove -Itest-nginx/lib -I. t/core/x-forwarded-host-repro.t # yaml_config without trusted_addresses
Failed tests: 6, 10, 14 # headers.remove, headers.set "", headers.set <value>
Result: FAIL
Defect 2 — X-Forwarded-Host cannot be deleted at all (pre-existing)
set_upstream_x_forwarded_headers() only ever writes a value:
local host = api_ctx.var.http_x_forwarded_host
if host then
api_ctx.var.var_x_forwarded_host = host
end
When the request header is gone — headers.remove, or headers.set with "", which OpenResty treats as removal — host is nil, the branch is skipped, and $var_x_forwarded_host keeps the location-level default set $var_x_forwarded_host $host; (apisix/cli/ngx_tpl.lua:878). The original host is forwarded regardless of trust. Fixing defect 1 does not fix this; there is currently no "unset" path.
This one predates #12551 and is better described as a gap than a regression, but it does contradict the proxy-rewrite documentation, which presents headers.remove as removing headers before the request reaches the upstream.
Workarounds available today
On 3.17.0, add the network your requests arrive from to apisix.trusted_addresses. That restores both proxy-rewrite overrides and the before_proxy / ngx.var.var_x_forwarded_host approach (verified above). Note this also changes how inbound X-Forwarded-* headers are treated, so it is only appropriate when that peer really is a trusted hop such as your own ingress.
Deleting the header outright still requires the before_proxy route:
plugins:
serverless-pre-function:
phase: before_proxy
functions:
- "return function(conf, ctx) ngx.var.var_x_forwarded_host = '' end"
An empty $var_x_forwarded_host makes nginx omit the header entirely. This needs trusted_addresses on 3.17.0; on master it works either way.