Advanced HTTPRoute Filters in Kubernetes Gateway API¶
This document provides a detailed reference on how to use HTTPRoute with advanced filters in the Gateway API. It includes real-world use cases and complete YAML examples.
๐ Prerequisites¶
Ensure you have the following deployed:
- A Gateway controller (e.g., NGINX Gateway)
- A Gateway resource named
my-nginx-gatewayin namespacenginx-gateway
Each HTTPRoute will reference this Gateway using:
parentRefs:
- name: my-nginx-gateway
namespace: nginx-gateway
sectionName: http
๐ Filter Use Cases¶
1. ๐ฆ Request Redirection (HTTP โ HTTPS)¶
rules:
- filters:
- type: RequestRedirect
requestRedirect:
scheme: https
statusCode: 301
Redirects all incoming HTTP requests to HTTPS.
2. ๐ URL Rewrite (/old โ /new)¶
rules:
- matches:
- path:
type: PathPrefix
value: /old
filters:
- type: URLRewrite
urlRewrite:
path:
replacePrefixMatch: /new
backendRefs:
- name: my-app
port: 80
Rewrites
/oldpaths to/newbefore forwarding to backend.
3. ๐ง Request Header Modification¶
rules:
- filters:
- type: RequestHeaderModifier
requestHeaderModifier:
add:
x-env: staging
set:
x-country: PK
remove:
- x-remove-this
backendRefs:
- name: my-app
port: 80
Adds, sets, and removes headers in the request before it hits the backend.
4. ๐ช Request Mirroring¶
rules:
- filters:
- type: RequestMirror
requestMirror:
backendRef:
name: mirror-service
port: 80
backendRefs:
- name: my-app
port: 80
Sends a copy of the request to
mirror-servicewithout affecting the original flow.
5. ๐งฐ Combined Filters¶
rules:
- matches:
- path:
type: PathPrefix
value: /products
filters:
- type: URLRewrite
urlRewrite:
path:
replacePrefixMatch: /items
- type: RequestHeaderModifier
requestHeaderModifier:
add:
x-service-version: v2
backendRefs:
- name: product-service
port: 8080
Chains rewrite and header modification for advanced routing.
6. ๐งช Default Basic Match¶
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: frontend-svc
port: 80
Acts as a fallback catch-all route.
๐งฑ Filter Summary¶
| Filter Type | Description |
|---|---|
RequestRedirect | Redirects requests (e.g., HTTP โ HTTPS) |
URLRewrite | Rewrites the path before sending to backend |
RequestHeaderModifier | Add/Set/Remove headers on incoming requests |
RequestMirror | Mirrors request to another backend silently |
ExtensionRef | Custom filters (defined by controller vendors) |
๐ง Tip¶
You can define multiple rules in one HTTPRoute, each with different matches and filters for microservices routing, A/B testing, canary deployments, and more.