Advanced
This page contains examples demonstrating advanced routing patterns in Gateway API, including HTTP method matching, query parameter matching, and complex matching logic combining multiple conditions.
Advanced Matching
Gateway API supports sophisticated matching capabilities that allow you to route traffic based on multiple criteria simultaneously. These patterns are native to Gateway API, unlike Nginx Ingress which requires server snippets for many of these features.
Examples
HTTP Method Matching
Routes traffic based on HTTP method (GET, POST, PUT, DELETE, etc.). Useful for separating read and write operations to different backends.
Key Points:
- Match on HTTP methods: GET, POST, PUT, DELETE, PATCH, OPTIONS, etc.
- Multiple methods in one match create OR logic (any of these)
- Useful for separating read/write operations
- Can combine with path matching for more precise routing
Common Use Cases:
- Separate read and write services
- Route CORS preflight (OPTIONS) to specific service
- Different backends for different HTTP methods
Query Parameter Matching
Routes based on URL query parameters. Native in Gateway API, requires server snippets in Nginx Ingress.
Key Points:
- Match on query parameters (e.g.,
?version=v2) - Supports exact matching and regular expressions
- Useful for API versioning and feature flags
- Can combine with other match conditions
Common Use Cases:
- API versioning via query params (
?version=v2) - Environment routing (
?env=staging) - Feature flags (
?feature=new-ui) - A/B testing (
?variant=a)
Complex Matching Logic
Shows how to combine path, headers, query params, and methods for sophisticated routing rules.
Key Concepts:
-
AND Logic (within a match block): All conditions in a single match block must be true
- matches: - path: /api headers: - name: X-Environment value: staging method: POSTAll of these must match: path AND header AND method
-
OR Logic (multiple match blocks): Any match block can match
- matches: - path: /api headers: - name: X-Environment value: staging - path: /api headers: - name: X-Environment value: developmentEither staging OR development can match
Key Points:
- Combine multiple match types: path, headers, query params, methods
- Use AND logic for precise routing
- Use OR logic for flexible routing
- Rules are evaluated in order (first match wins)
Matching Types
Gateway API supports matching on:
- Path: PathPrefix, Exact, RegularExpression
- Headers: Exact, RegularExpression
- Query Parameters: Exact, RegularExpression
- HTTP Method: GET, POST, PUT, DELETE, PATCH, OPTIONS, etc.
- Hostname: Exact match on hostname
Best Practices
- Order Matters: Place more specific rules before less specific ones
- Combine Conditions: Use AND logic for precise routing
- Use OR for Flexibility: Multiple match blocks for flexible routing
- Test Thoroughly: Complex matching can be hard to debug - test all scenarios
- Document Logic: Add comments explaining complex routing rules
- Performance: More complex matching may add latency - profile if needed
- Default Route: Always include a default/catch-all route
Common Patterns
Pattern 1: Environment + Path Routing
- matches:
- path:
type: PathPrefix
value: /api
headers:
- name: X-Environment
value: staging
Pattern 2: Method + Path Separation
- matches:
- path:
type: PathPrefix
value: /api
method: POST
Pattern 3: Query Parameter Versioning
- matches:
- queryParams:
- name: version
value: v2
Pattern 4: Complex Multi-Condition
- matches:
- path:
type: PathPrefix
value: /api
headers:
- name: X-User-Type
value: premium
method: POST
queryParams:
- name: version
value: v2
Related Documentation
- Advanced Routing Documentation - Detailed explanation of advanced matching
- Core Concepts - Understanding Gateway API fundamentals