Support for modifying request headers
Description
In the routes configuration section we have the possibility to add/set/remove response headers.
I think we need a similar feature to handle request headers.
Use cases
Pass $request_id to the application so it can be attached to the logs to improve traceability:
{
"match": {
"uri": "/admin/*"
},
"action": {
"pass": "applications/my_application/backoffice",
"request_headers": {
"Request-ID": "$request_id"
}
}
}
Handle X-Forwarded-Prefix:
{
"match": {
"uri": "/api/*"
},
"action": {
"rewrite": `${uri.substr('/api'.length)}`,
"request_headers": {
"X-Forwarded-Prefix": "/api"
},
"pass": "applications/my_application/api"
}
}
Related issues and discussions
This topic was discussed in #1183 There is a similar request in #1021
If this is implemented can it also support the use case to not overwrite an existing header? The use case is for a correlation id which if provided should be overwritten by the current request id.
Thanks @txigreman for creating this issue. I followed the discussions and the issue that we are currently don't support dynamic values or values created by JavaScript functions in our environment configuration section.
The feature is a little bit more complicated than just adding headers to the application context as described in the discussion. But having this available including dynamic values is indeed a great add-on to Units capabilities. We are currently spending some time defining a scope for OpenTelemetry Support for Unit and a header with a trace or span ID will be one part of the solution to inform the application about the necessary data.
Thanks @txigreman for reporting / suggesting this and @tippexs for picking it up.
Stumbled upon the same problem, creating an app with Go Fiber and rewriting URL's in Unit. Ideally can have Unit pass in X-Forwarded-[X] headers, working around the problem using an ENV to simulate this atm. Less clean as I like it to be :)
Yeah, I'd like to +1 this especially now that rewriting the REQUEST_URI is no longer permitted (https://github.com/nginx/unit/commit/87077ec4ba9a59f3332c3758a3c39a5c149025e5).
Being able to send an X_REQUEST_URI instead would be great
How to implement adding a header to a request in nginx unit that will be passed to php?
map $http_x_request_id $reqid { default $http_x_request_id; "" $request_id; }
Variables not work in applications/*/environment section
"applications": {
"php": {
"environment": {
"HTTP_X_REQUEST_ID": "$request_id",
"MY_URI": "$uri"
},
in PHP show as text
HTTP_X_REQUEST_ID $request_id MY_URI $uri