caddy: reverse_proxy handler: cannot delete header
I’m trying to delete header in a reverse_proxy handler, but it looks like there’s some bug that prevents it.
Here is the minimal config to reproduce it:
{
"apps": {
"http": {
"servers": {
"public": {
"listen": [":80"],
"routes": [
{
"match": [
{
"path": ["/generate_204"]
}
],
"handle": [
{
"handler": "reverse_proxy",
"upstreams": [
{
"dial": "google.com:80"
}
],
"headers": {
"response": {
"delete": ["Server"],
"deferred": true
}
}
}
],
"terminal": true
}
]
}
}
}
}
}
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (9 by maintainers)
@condemil Middleware are handlers. We won’t be differentiating because what do those terms even mean? If a middleware is a handler that doesn’t terminate the chain (and doesn’t write the respones), and a handler is a middleware that terminates the chain (and writes the response), then what is a cache handler? If it’s a cache hit, it’s a handler, otherwise it’s a middleware. So splitting them out in config doesn’t make any sense and that’s why we didn’t do that. It’s much simpler if you think of handlers as just layers that requests go through.
Our JSON docs describe this in detail (it’s more relevant with the JSON, where all routes are explicitly ordered): https://caddyserver.com/docs/json/apps/http/servers/routes/#handle
It’s set by the HTTP server. The
headershandler only manipulates headers when it is configured. As a rule of thumb, if your config doesn’t list a handler, it’s not in the chain.The
reverse_proxyhandler has the ability to manipulate headers that go to or come from the backend only, which is useful when you want to differentiate which response headers you allow, since in the reverse proxy handler, you know which headers come from the backend. Whereas outside of it, it’s hard to know whether it was set by the backend or by this instance. It makes sense that the reverse_proxy module manipulates proxied headers. It would be confusing if it manipulated any/all headers.Ok now I got it, some “handlers” is called handler, but it’s actually a middleware. Maybe if there will be caddy v3 it makes sense to extract middleware to a separate
middlewareentity in configuration to make it clearer. Thank you for clarifying!It is set outside of any handler module. It’s set at the first stage when Caddy receives the request and before the handler chain is executed:
https://github.com/caddyserver/caddy/blob/7672b7848f196e1baa859bcc2cf6610546097947/modules/caddyhttp/server.go#L279
However, the user can remove it by using the
headershandler.The addition of the
Serverheader is implicitly done by all web servers, and it isn’t something out of the ordinary that Caddy does. It isn’t a surprise in other web servers and shouldn’t be a surprise in Caddy. It’s arguably a positive value to the ecosystem to recognize which software runs on the web for data and analysis amongst others. In Caddy, you can simply delete it by using theheadershandler, while on others you have to compile them from source to remove it.You used the header manipulation inside
reverse_proxy, which is intended to manipulate headers going up or coming down thereverse_proxyaspect. TheServerheader is not part of thereverse_proxydata going up/down. It’s outsidereverse_proxy.@steffenbusch, in your case, that’s intended behavior per the commit I linked earlier and based on the discussion in #6275.
@condemil, you’re trying to manipulate the header inside the
reverse_proxyresponse. However, theServerheader is not set insidereverse_proxy. You should use theheadershandler. As mentioned earlier, this is not a bug and is a config issue.I would consider my observed behaviour (and not yet digged into it in detail) different to yours.
I have tested your json config (just changed 80 to 8080) against caddy-v.2.9.1:
Testing same json config against caddy-v2.10.0-beta2:
The only tangentially related change I see is https://github.com/caddyserver/caddy/commit/e7da3b267bcec986aaca960dd22ef834d3b9d4a6, and the code doesn’t seem to cause the described impact.
Because we know it works per existing users and tests. This is likely a configuration/usage issue rather than a bug. A bug like this is unlikely to have survived so many versions without being noticed. Many of Caddy users delete headers, and their configuration works.