1. The problem I’m having:
I’m trying to set a request header when using reverse_proxy
, based on a different request header. I thought that a good approach would be to create a new Caddy module which implements a new template function to hand the complexity of transforming the headers.
Unfortunately, I can’t seem to be able to use templates when setting up new headers. I know that I can use {{.RespHeader.Set ...}}
to set headers while using a template, for example when performing a static_request
, but that’s not what I want to do, I want to do it on a reverse_proxy
.
2. Error messages and/or full log output:
Here’s an example of a test configuration file:
{
"apps": {
"http": {
"servers": {
"test": {
"listen": [
":8080"
],
"routes": [
{
"handle": [
{
"handler": "templates"
},
{
"handler": "vars",
"string_one": "{{randAlpha 4}}"
},
{
"handler": "headers",
"response": {
"set": {
"string_one": ["{http.vars.string_one}"],
"string_two": ["{{randAlpha 4}}"]
}
}
},
{
"handler": "static_response",
"body": "{{randAlpha 4}} {http.vars.string_one}\n"
}
]
}
]
}
}
}
}
}
This is the result:
# curl -v localhost:8080
* Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.88.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 7
< Content-Type: text/plain; charset=utf-8
< Server: Caddy
< String_one: }
< String_two: {{randAlpha 4}}
< Date: Wed, 23 Aug 2023 16:20:19 GMT
<
YLOq }
* Connection #0 to host localhost left intact
Interesting things:
- Templating works fine inside the
static_response
- Templating doesn’t work at all inside the
headers
- Templating is super weird inside
vars
, cutting the string. Maybe it things it’s supposed to be a placeholder?
So I’m concluding that templating is not at all universal and only works under selected handlers. Is there a way to template a header when using reverse_proxy
?
I’ve also tried setting up headers
inside reverse_proxy
, with the same result.
Thank you
3. Caddy version:
v2.7.4
4. How I installed and ran Caddy:
a. System environment:
Downloaded Caddy from the website, under Linux.
b. Command:
./caddy run -c caddy.json