Logging a Response Header Field in Caddy Reverse Proxy without Returning to Client

1. The problem I’m having:

How can I read a specific field from the Response Header in a reverse proxy with Caddy and write it to a log, without returning it to the client?

For example, I want to log the UserId in my logs. I return the X-User-Id field in the reverse_proxy directive, hoping to write it to the log. Then, I use the header -X-User-Id directive to remove this field so that it is not returned to the client. However, once I remove this header, it is also absent in the logs.

2. Error messages and/or full log output:

/

3. Caddy version:

2.7

4. How I installed and ran Caddy:

a. System environment:

Docker

b. Command:

sudo docker compose up -d

c. Service/unit/compose file:

...

d. My complete Caddy config:

example.com {
	reverse_proxy myservice:8080

	log {
		output file /var/log/myservice/access.log
	}

	# Remove header
	header {
		-X-User-Id
	}
}

5. Links to relevant resources:

It’s not possible yet, but we’re working on it:

This isn’t what you’re asking for, but you could enable the debug global option to get the reverse_proxy logs, which do contain the proxy’s response headers before being written to the client. You could configure a logger in global options to only include those logs from http.handlers.reverse_proxy in one file.

1 Like

I had another idea which might be a little nicer, i.e. not an extra log field, but just reusing the existing user_id one in the access logs:

2 Likes

It’s cool, can it be designed to be more flexible and dynamic, not only for scenarios that require logging UserId, but also for others, such as X-Project-Id/X-Team-Id?"

extra_log (or whatever it ends up being called) will allow you to do that, yes.

1 Like

Great, I’ll look forward to extra_log (or whatever it ends up being called)…

Thank you very much for your reply.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.