Skip to content

Commit 4b0aa63

Browse files
authored
NGF: Update advanced routing guide for Regex PathType (#1286)
feat: Update advanced routing guide for regex path
1 parent 6822326 commit 4b0aa63

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

content/ngf/traffic-management/advanced-routing.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Learn how to deploy multiple applications and HTTPRoutes with request conditions
1111

1212
## Overview
1313

14-
In this guide we will configure advanced routing rules for multiple applications. These rules will showcase request matching by path, headers, query parameters, and method. For an introduction to exposing your application, we recommend that you follow the [basic guide]({{< ref "/ngf/traffic-management/basic-routing.md" >}}) first.
14+
In this guide we will configure advanced routing rules for multiple applications. These rules will showcase request matching by path (including prefix, exact, and regex patterns), headers, query parameters, and method. For an introduction to exposing your application, we recommend that you follow the [basic guide]({{< ref "/ngf/traffic-management/basic-routing.md" >}}) first.
1515

1616
The following image shows the traffic flow that we will be creating with these rules.
1717

@@ -321,6 +321,67 @@ Server name: tea-post-b59b8596b-g586r
321321

322322
This request should receive a response from the `tea-post` pod. Any other type of method, such as PATCH, will result in a `404 Not Found` response.
323323

324+
## Path matching types
325+
326+
NGINX Gateway Fabric supports three types of path matching:
327+
328+
- **PathPrefix**: Matches based on a URL path prefix split by `/`. For example, `/coffee` matches `/coffee`, `/coffee/`, and `/coffee/latte`.
329+
- **Exact**: Matches the exact path in the request. For example, `/coffee` matches only `/coffee`.
330+
- **RegularExpression**: Matches based on RE2-compatible regular expressions. For example, `/coffee/[a-z]+` matches `/coffee/latte` and `/coffee/mocha` but not `/coffee/123`.
331+
332+
{{< call-out "note" >}} Regular expression path matching uses the RE2 syntax. Patterns are automatically anchored to the beginning of the path. {{< /call-out >}}
333+
334+
### Example: Using regex path matching
335+
336+
To route requests based on regex patterns in the path, use `type: RegularExpression`:
337+
338+
```yaml
339+
kubectl apply -f - <<EOF
340+
apiVersion: gateway.networking.k8s.io/v1
341+
kind: HTTPRoute
342+
metadata:
343+
name: coffee-regex
344+
spec:
345+
parentRefs:
346+
- name: cafe
347+
hostnames:
348+
- cafe.example.com
349+
rules:
350+
- matches:
351+
- path:
352+
type: RegularExpression
353+
value: /coffee/[a-z]+
354+
backendRefs:
355+
- name: coffee-v1-svc
356+
port: 80
357+
EOF
358+
```
359+
360+
This configuration routes requests like `/coffee/latte` or `/coffee/mocha` to the `coffee-v1-svc` backend, while paths like `/coffee/123` or `/coffee` will not match.
361+
362+
#### Send traffic using regex paths
363+
364+
You can test the regex path matching with curl:
365+
366+
```shell
367+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee/latte
368+
```
369+
370+
This request should receive a response from the `coffee-v1` Pod since `/coffee/latte` matches the pattern `/coffee/[a-z]+`.
371+
372+
```text
373+
Server address: 10.244.0.9:8080
374+
Server name: coffee-v1-76c7c85bbd-cf8nz
375+
```
376+
377+
However, a request with a numeric path segment will not match:
378+
379+
```shell
380+
curl --resolve cafe.example.com:$GW_PORT:$GW_IP http://cafe.example.com:$GW_PORT/coffee/123
381+
```
382+
383+
This will result in a `404 Not Found` response since `/coffee/123` does not match the pattern `/coffee/[a-z]+`.
384+
324385
## Troubleshooting
325386

326387
If you have any issues while sending traffic, try the following to debug your configuration and setup:

0 commit comments

Comments
 (0)