Skip to content

Header rules

A header rule controls which request headers the gateway forwards to the backend, and lets you remove or add headers on the way.

Header rules are defined under Rules.HeaderRules and bound by name with HeaderRule on GlobalConfig, a server or a route. The closest scope wins: route, then server, then global. When no rule applies, all headers are forwarded.

Rules:
HeaderRules:
allowlist-headers:
Mode: Custom
CustomHeadersList: [X-Request-Id, X-Trace-Id]
strip-and-set:
Mode: All
ForwardCookies: false
Remove: [X-Internal]
Set:
X-Source: gateway
preserve-host:
Mode: All
PreserveHost: true
Key Type Default Description
Mode All or Custom required All forwards every header. Custom forwards only the headers in CustomHeadersList.
CustomHeadersList list of header names none Headers forwarded when Mode is Custom.
ForwardAuthorization true / false true false removes Authorization, X-Api-Key and X-Auth-Token. To remove the credential of an authentication method, including a custom HeaderName, use RemoveCredentialHeader on the method.
ForwardCookies true / false true false removes Cookie.
Remove list of header names none Headers removed from the request.
Set map of header name to value none Headers added to the request, replacing any existing value.
PreserveHost true / false false true sends the client’s Host header to the backend instead of the backend’s own host.
  1. Mode and CustomHeadersList select the headers to keep.
  2. ForwardAuthorization and ForwardCookies remove the credential headers when set to false.
  3. Remove removes the listed headers.
  4. Set adds the listed headers.
  5. The gateway adds its own X-Nocleye-* headers, and the auth method’s RemoveCredentialHeader removes its credential header.

Because Set runs last among the rule steps, a header you set is always sent, even if an earlier step removed it.

Headers that are always kept in Custom mode

Section titled “Headers that are always kept in Custom mode”

Content-Type, Content-Length and Content-Encoding are forwarded even when they are not in CustomHeadersList, because dropping them would break request bodies. The gateway also always sends a Traceparent.

On a route to a gRPC or WebSocket server more headers are kept, because the protocol breaks without them:

Server Always kept in Custom mode
GRPC Te, Grpc-Timeout, Grpc-Encoding, Grpc-Accept-Encoding, Grpc-Message-Type, Grpc-Previous-Rpc-Attempts (Te: trailers is what makes a gRPC backend answer).
WebSocket Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol, Sec-WebSocket-Extensions.

Remove and Set of these names on a route to such a server is an error in nocleye check, instead of a silent call that never works.

  • Names are case insensitive, x-trace-id and X-Trace-Id are the same header.
  • Names can’t be empty and can’t contain underscores or other characters that are not valid in a header name.
  • Headers reserved for the gateway can’t be listed in CustomHeadersList: names starting with X-Nocleye-, and X-User-Id, X-User, X-User-Email, X-User-Roles, X-Tenant, X-Tenant-Id, X-Roles, X-Role, X-Client-Id, X-Caller-Id, X-Authenticated, X-Debug and X-Debug-Forwarded.

The gateway adds these headers to the request it sends to the backend. They are set after the header rule runs, so Custom mode, Remove and ForwardAuthorization don’t affect them, and Set can’t overwrite them.

Header Value
X-Nocleye-Authenticated true when the route’s authentication method validated a credential, false otherwise. Sent on every proxied request, including routes with no authentication method.
X-Nocleye-User-Id The caller identity of an authenticated request (see caller identity). Not sent for anonymous requests.
X-Nocleye-Claim-<Suffix> Token claims selected with ForwardClaims. Only on authenticated requests.
X-Nocleye-Request-Id The gateway’s id of the request, see observability.

Any X-Nocleye-* header sent by a client is dropped, so the backend can trust these.

To stop clients from impersonating an identity, the gateway always drops, regardless of the rule:

  • any header whose name contains an underscore,
  • the reserved headers listed above.

The only way to send one of the reserved headers to a backend is to add it yourself with Set.

The gateway always sends a Traceparent header to the backend, in every mode, including Custom:

  • If the client sent a valid Traceparent (00-<trace id>-<span id>-<flags>), the gateway continues the trace: the backend receives the same trace id and flags, and a new span id generated by the gateway.
  • If the client sent none, or an invalid one, the gateway starts a new trace.

Traceparent does not need to be in CustomHeadersList, and Remove, ForwardAuthorization and ForwardCookies don’t affect it. It can’t be changed with Set or AddHeader. Tracestate is not generated or changed by the gateway, it follows the rule like any other header.

nocleye check rejects a Set entry when:

  • the name starts with X-Nocleye-, because those headers carry the real request and user identity produced by the gateway,
  • the name is managed by HTTP or the proxy: Connection, Keep-Alive, Transfer-Encoding, Upgrade, Content-Length, Content-Encoding, Host, Expect, Forwarded, X-Forwarded-*, Traceparent and similar,
  • the value contains line breaks or other control characters.

Identity headers like X-User-Id or X-Tenant can still be set, for example to send a fixed value to one backend.