nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.37k stars 323 forks source link

Support for modifying request headers #1189

Open txigreman opened 6 months ago

txigreman commented 6 months ago

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

amne commented 6 months ago

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.

tippexs commented 6 months ago

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.

johanjanssens commented 6 months ago

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 :)