> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Header Based Routing for AB Testing

> Using Gateway API HTTPRoute matches to route requests by headers, paths, queries, and methods for A/B testing and progressive feature rollouts with NGINX Fabric Gateway.

Header-based routing directs requests to different backends by inspecting request attributes (headers, cookies, query parameters, path, HTTP method, etc.). Compared with weight-based routing, header-based routing gives you finer-grained control for A/B experiments and progressive feature rollouts because you can evaluate any information the client sends and apply precise rules to route traffic.

A common A/B test use case: you want to test a redesigned registration flow only for *new* customers. The gateway or an upstream service validates whether a user is new (by consulting cookies, caches, or other data sources) and then injects an attribute such as `new-customer: true`. The HTTP gateway uses that attribute to route the request to the new registration backend; other users continue to the existing flow—transparent to the end user.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/QZ7pWzRtYdnRAGco/images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/customer-new-old-gateway-httproute-flag.jpg?fit=max&auto=format&n=QZ7pWzRtYdnRAGco&q=85&s=ff756b2b0fc4b3607d9d3e320d33429c" alt="A simple architecture diagram showing customers classified as &#x22;New&#x22; or &#x22;Old&#x22; (validated by data sources), with a gateway adding a &#x22;new-customer: true/false&#x22; flag. Requests are routed via an HTTPRoute to either the App — New Version or App — Old Version." width="1920" height="1080" data-path="images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/customer-new-old-gateway-httproute-flag.jpg" />
</Frame>

When the backend receives the registration request, it gets the full request body plus any flags or headers the gateway added (for example, `new-customer: true`). The backend uses that metadata to complete the appropriate registration flow.

In an NGINX Fabric Gateway environment, you implement this behavior with the Gateway API’s HTTPRoute resource. An HTTPRoute rule defines:

* one or more matches (what attributes to inspect), and
* backendRefs (where to send matching requests).

Matches can include path, headers, query parameters, and HTTP methods. You can also apply filters and other Gateway API features as needed.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/QZ7pWzRtYdnRAGco/images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/nginx-fabric-gateway-httproute-backends.jpg?fit=max&auto=format&n=QZ7pWzRtYdnRAGco&q=85&s=d0c9409d67daf47ed82c2521cecdc1da" alt="A simple architecture diagram of an NGINX Fabric Gateway routing client (browser/API) requests via an HTTPRoute. The route uses Matches, Filters and BackendRefs to forward traffic to Service A, Service B, and an older app version." width="1920" height="1080" data-path="images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/nginx-fabric-gateway-httproute-backends.jpg" />
</Frame>

At a high level:

* A match declares what to look for (path, header, method, query).
* backendRefs list the target services for traffic that meets those matches.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/QZ7pWzRtYdnRAGco/images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/matches-backendrefs-routing-rules.jpg?fit=max&auto=format&n=QZ7pWzRtYdnRAGco&q=85&s=ac08fb313171cb0312471bb1d35f98c6" alt="A minimalist slide showing two rounded boxes labeled &#x22;Matches&#x22; and &#x22;BackendRefs.&#x22; The &#x22;Matches&#x22; box explains what to use, what value to expect, and where to route traffic, while the &#x22;BackendRefs&#x22; box notes declaring backendRefs within the &#x22;rules&#x22; setting." width="1920" height="1080" data-path="images/Gateway-API-with-NGINX-Fabric-Gateway/Advanced-Traffic-Management/Header-Based-Routing-for-AB-Testing/matches-backendrefs-routing-rules.jpg" />
</Frame>

You can combine multiple match criteria to build precise rules. For example, route requests whose path starts with `/coffee` and that include a header `version: v2` to `coffee-v2`; route other `/coffee` requests to `coffee-v1`.

Note: the YAML snippets below are conceptual fragments showing match logic. In an actual HTTPRoute resource, `matches` are nested under a `rules` entry and `backendRefs` are declared at the rule level. See the Gateway API schema for exact field layout and supported match options: [https://gateway-api.sigs.k8s.io/references/spec/](https://gateway-api.sigs.k8s.io/references/spec/).

Example: path + header match (route to coffee-v2 when header `version: v2` is present)

```yaml theme={null}
- matches:
  - path:
      type: PathPrefix
      value: /coffee
    headers:
      - name: version
        value: v2
  backendRefs:
    - name: coffee-v2
      port: 80
```

Example: path-only match (route to coffee-v1 for `/coffee` requests that do not match the header-based rule)

```yaml theme={null}
- matches:
  - path:
      type: PathPrefix
      value: /coffee
  backendRefs:
    - name: coffee-v1
      port: 80
```

Method-based routing is another option (less commonly used for A/B testing). For example, route `POST` requests to a specific backend:

```yaml theme={null}
- matches:
  - method: POST
  backendRefs:
    - name: coffee-v1
      port: 80
```

Because Gateway API implementations evaluate matches according to their semantics (for example, longest prefix, specificity, or explicit rule order), design your rules to avoid unintended overlaps. Prefer specific matches (new flows) before more general fallbacks (existing flows).

Table: common match types and typical use cases

| Match type | When to use | Example snippet |
| - | - | - |
| Path | Route by URL structure (e.g., feature endpoints) | `path: /coffee` |
| Header | Target users, versions, or flags (`Cookie` or custom headers) | `headers: - name: version value: v2` |
| Query parameter | A/B test via URL parameters (e.g., `?test=true`) | `queryParams: - name: test value: true` |
| Method | Differentiate by HTTP verbs (e.g., POST vs GET) | `method: POST` |

<Callout icon="lightbulb" color="#1CB2FE">
  You can combine path, header, query-parameter, and method matches in a single HTTPRoute match. Use the most specific rules first (for new flows) and fall back to broader rules (legacy flows) to prevent ambiguous routing.
</Callout>

References and further reading

* Gateway API specification: [https://gateway-api.sigs.k8s.io/spec/](https://gateway-api.sigs.k8s.io/spec/)
* Gateway API reference: [https://gateway-api.sigs.k8s.io/references/spec/](https://gateway-api.sigs.k8s.io/references/spec/)
* NGINX Fabric Gateway documentation (for product-specific filters and behavior)

That’s the core idea: use header-based matching (and other request attributes) in HTTPRoute rules to implement targeted A/B tests and controlled rollouts in an NGINX Fabric Gateway / Gateway API environment.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/gateway-api-with-nginx-fabric-gateway/module/b4f1d9ae-8b89-4650-a5e1-6665008f40f8/lesson/e8293575-8c2b-4ee5-ba98-f20bae7d1db8" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.