Header Routing
This page contains examples demonstrating header-based routing in Gateway API. Header routing allows you to route traffic based on HTTP headers, which is useful for A/B testing, feature flags, environment routing, and user segmentation.
Header Matching
Gateway API supports native header matching, which is a significant advantage over Nginx Ingress where header-based routing requires custom server snippets.
Examples
Basic Header Matching
Routes traffic based on a single header value. This example routes to different services based on the X-Environment header.
Key Points:
- Header matching is native in Gateway API (no annotations needed)
- Headers are matched exactly by default
- Can combine header matching with path matching
- Default route handles requests without matching headers
Multiple Header Conditions
Shows AND logic (all headers must match) and OR logic (multiple matches). Demonstrates complex routing scenarios.
Key Points:
- AND Logic: All headers in a single
headersarray must match - OR Logic: Multiple match blocks - any one can match
- Useful for feature flags, user segmentation, and environment routing
- Can combine with path matching for complex routing rules
Ingress Equivalent
For comparison, here's the equivalent Nginx Ingress configuration:
Note: Nginx Ingress requires server snippets for header routing, which is Nginx-specific and not portable.
Use Cases
- Environment Routing: Route to staging/production based on headers
- Feature Flags: Enable features for specific users or groups
- A/B Testing: Route traffic to different service versions
- User Segmentation: Route premium users to different backends
- Canary Deployments: Gradually route traffic to new versions
Best Practices
- Use Descriptive Header Names: Use clear, consistent header names (e.g.,
X-Environment,X-User-Type) - Combine with Path Matching: Use headers together with paths for more precise routing
- Default Route: Always include a default route for requests without matching headers
- Header Values: Be consistent with header value formats (case-sensitive)
- Security: Don't rely on headers for security - validate on the backend
Related Documentation
- Advanced Routing Documentation - Detailed explanation of header matching
- Core Concepts - Understanding Gateway API fundamentals