Sub topic 8
Redirects, rewrites and header changes
This chapter assumes you know the Ingress resource, have an HTTPRoute attached to a Gateway, and can read basic NGINX annotations. In Ingress NGINX, redirects, rewrites and header edits are annotations on the Ingress. In the Gateway API they are filters, which are typed entries in a rule's filters list. Filters make these behaviors part of the API schema, so they validate and move between implementations.
Where filters live
A filter belongs to a rule in an HTTPRoute. It applies to every request that matches that rule. Filters run in the order the implementation defines, so don't rely on one filter seeing another's output.
Annotation to filter mapping
| Ingress NGINX annotation | Gateway API filter |
|---|---|
| `ssl-redirect: "true"` | `RequestRedirect` with `scheme: https` |
| `permanent-redirect` | `RequestRedirect` with `statusCode: 301` |
| `rewrite-target` | `URLRewrite` |
| `proxy-set-headers`, `configuration-snippet` for headers | `RequestHeaderModifier` |
| `add_header` in a snippet | `ResponseHeaderModifier` |
Redirecting HTTP to HTTPS
The ssl-redirect annotation becomes a separate route on the plain-HTTP listener.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: app-https-redirect
namespace: shop
spec:
parentRefs:
- name: shared-gateway
namespace: gateway-system
sectionName: http
hostnames:
- app.example.com
rules:
- filters:
- type: RequestRedirect
requestRedirect:
scheme: https
statusCode: 301The route has no backendRefs, because a redirect answers the client directly. It attaches to the http listener only, so HTTPS traffic never reaches it.
Rewriting paths
Suppose the old Ingress used rewrite-target: / to strip an /api prefix. The equivalent uses URLRewrite with a prefix replacement.
rules:
- matches:
- path:
type: PathPrefix
value: /api
filters:
- type: URLRewrite
urlRewrite:
path:
type: ReplacePrefixMatch
replacePrefixMatch: /
backendRefs:
- name: api
port: 8080A request for /api/orders reaches the api Service as /orders. This replaces the common regex-and-capture-group pattern in rewrite-target. Arbitrary regex rewrites have no standard form, so check them against the annotation chapter later in this tutorial.
Changing headers
Header filters add, set or remove headers.
filters:
- type: RequestHeaderModifier
requestHeaderModifier:
set:
- name: X-Environment
value: production
add:
- name: X-Request-Source
value: gateway
remove:
- X-Debug
- type: ResponseHeaderModifier
responseHeaderModifier:
set:
- name: Strict-Transport-Security
value: max-age=31536000set overwrites an existing header, add appends a value, and remove deletes the header.
Common errors
- Putting a redirect route on the HTTPS listener. This can loop clients forever.
- Using
ReplaceFullPathwhere you meantReplacePrefixMatch, which sends every request to one path. - Expecting a redirect route to need a backend.
Exercise
An Ingress had rewrite-target: / on a rule matching /shop. Which filter and field do you use in an HTTPRoute, and what does a request for /shop/cart become?
Show answer
Use a URLRewrite filter with path.type: ReplacePrefixMatch and replacePrefixMatch: /, on a rule matching PathPrefix /shop. The request /shop/cart reaches the backend as /cart.