Header Modification

TOC

Basic Concepts

When a request is received, header modification allows adjusting request headers before forwarding to the backend. Similarly, when a response is received, it allows adjusting response headers before they are returned to the client.

use annotations

targetannotation key
ingressalb.ingress.cpaas.io/rewrite-request,alb.ingress.cpaas.io/rewrite-response
rulealb.rule.cpaas.io/rewrite-request, alb.rule.cpaas.io/rewrite-response

Annotation values are JSON strings containing the config.

type RewriteRequestConfig struct {
	Headers       map[string]string   `json:"headers,omitempty"`         // set header
	HeadersVar    map[string]string   `json:"headers_var,omitempty"`     // set header, which values are variable name 
	HeadersRemove []string            `json:"headers_remove,omitempty"`  // remove header
	HeadersAdd    map[string][]string `json:"headers_add,omitempty"`     // add header, which values could be multiple
	HeadersAddVar map[string][]string `json:"headers_add_var,omitempty"` // add header, which values could be multiple and which values are variable name
}
type RewriteResponseConfig struct {
	Headers       map[string]string   `json:"headers,omitempty"`         // set header
	HeadersRemove []string            `json:"headers_remove,omitempty"`  // remove header
	HeadersAdd    map[string][]string `json:"headers_add,omitempty"`     // add header, which values could be multiple
}

Note: in *_var maps the key is the header name and the value is the ALB context variable name. For example, add the following annotation to an Ingress:

alb.ingress.cpaas.io/rewrite-request: '{
    "headers_var": {
        "x-my-host": "http_host"
    }
}'

will add key x-my-host and value of request host header to request header. you can refer to the nginx variable for the variable names.

ALB provides additional variables:

variable namedescription
first_forward_or_remote_addrthe first forwarded address or the remote address, default is remote_addr
first_forwardthe first forwarded address , default is empty string

examples

To add an Authorization header from a cookie, you can use:

alb.ingress.cpaas.io/rewrite-request: '{"headers_var":{"Authorization":"cookie_auth_token"}}'

To set HSTS, you can use:

alb.rule.cpaas.io/rewrite-response: |
    { "headers": { "Strict-Transport-Security": "max-age=63072000; includeSubDomains; preload"} }