Rules
A rule in AI Gateway determines how incoming requests are matched and forwarded to services. Rules act as the traffic routing configuration that maps incoming requests to the appropriate services based on paths, methods, and other conditions.
Rule Components
A rule consists of:
- Path: The URL path pattern to match
- Service ID: The service that will handle matched requests
- Methods: Allowed HTTP methods
- Strip Path: Whether to remove the matched path prefix
- Active: Whether the rule is currently active
Rule Matching
Rules determine how requests are matched to services through three main matching mechanisms:
Path Matching
The path matching system provides multiple strategies for routing requests based on URL patterns. Exact path matching provides the most precise control, ensuring requests match specific URLs exactly as configured. For more flexible routing, the system supports prefix matching with wildcards, allowing a single rule to capture multiple similar paths. More complex routing needs can be addressed using regular expression patterns, enabling sophisticated pattern matching for special use cases. The system employs priority-based selection when multiple patterns match, ensuring the most specific and appropriate rule is selected for each request.
Method Matching
Method matching controls how HTTP methods are handled for each route. Rules can be configured to accept specific HTTP methods, ensuring endpoints only respond to intended operations. The system supports multiple method support, allowing a single rule to handle different types of requests on the same path. Administrators can implement method restrictions to limit which HTTP operations are allowed, providing an additional layer of security and control over API access.
Header Matching
Header matching provides advanced request routing based on HTTP headers. Rules can match based on headers, allowing routing decisions to be made on factors like API versions, client types, or custom metadata. The system includes support for multiple headers, enabling complex routing scenarios based on combinations of header values. Regular expression patterns can be applied to header values, providing flexible matching capabilities for complex header-based routing requirements.
Path Handling
Rules provide several path handling options:
Strip Path
When enabled, removes the matched portion of the path before forwarding:
Request: /api/v1/chat/completions
Matched Path: /api/v1
Forwarded Path: /chat/completions
Preserve Host
Controls whether to forward the original Host header to the upstream service.
Rule Priority
When multiple rules match a request, the AI Gateway employs a sophisticated priority system to determine which rule should be applied: