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)

Most upvoted comments

@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

So the Server header is set in headers handler or outside of it? If it’s set in headers handler it can be documented under handler docs to make it clear how caddy works. If it’s set outside of headers handler I don’t think anything can be done documentation-wise.

It’s set by the HTTP server. The headers handler 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_proxy handler 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 middleware entity 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 headers handler.

The addition of the Server header 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 the headers handler, 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 the reverse_proxy aspect. The Server header is not part of the reverse_proxy data going up/down. It’s outside reverse_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_proxy response. However, the Server header is not set inside reverse_proxy. You should use the headers handler. 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:

$ curl -s --dump-header -  http://localhost:8080/generate_204
HTTP/1.1 204 No Content
Cross-Origin-Resource-Policy: cross-origin
Date: Tue, 25 Mar 2025 13:18:58 GMT
Server: Caddy

Testing same json config against caddy-v2.10.0-beta2:

$ curl -s --dump-header -  http://localhost:8080/generate_204
HTTP/1.1 204 No Content
Cross-Origin-Resource-Policy: cross-origin
Date: Tue, 25 Mar 2025 13:19:24 GMT
Via: 1.1 Caddy

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.