Sub topic 7
Path types and matching behavior
This chapter assumes you can write a basic Ingress manifest with host rules and that you know HTTP paths and status codes. Every path entry in an Ingress rule needs a pathType. It decides how the request path is compared with the path value. The wrong choice is a common cause of unexpected 404 responses.
The three path types
The networking.k8s.io/v1 API requires pathType on every path. If you omit it, the API server rejects the manifest. The allowed values are:
Exactmatches only the identical path, and the comparison is case-sensitive.Prefixmatches on a path prefix, compared element by element, and is also case-sensitive.ImplementationSpecificleaves matching to the Ingress controller.
For Exact and Prefix, the path value must start with /.
How Prefix matching works
Prefix matching does not compare characters. The controller splits both the configured path and the request path on / and compares the resulting elements one at a time. A request matches if every element of the configured path matches the start of the request path. A trailing slash is ignored.
The table shows how a few configured paths behave.
| Type | Configured path | Request path | Matches? |
|---|---|---|---|
| Prefix | `/` | `/anything/at/all` | Yes |
| Prefix | `/api` | `/api` | Yes |
| Prefix | `/api` | `/api/` | Yes |
| Prefix | `/api` | `/api/v1/users` | Yes |
| Prefix | `/api` | `/apis` | No, `apis` is a different element |
| Prefix | `/api/v1` | `/api` | No, the request is shorter |
| Exact | `/api` | `/api` | Yes |
| Exact | `/api` | `/api/` | No |
| Exact | `/api` | `/API` | No, matching is case-sensitive |
The /apis row is the most useful one to remember. A Prefix of /api is not the same as a string prefix.
When several paths match
A request can match more than one path in the same host rule. Kubernetes defines the winner as follows:
- The longest matching path wins.
- If two matching paths have the same length,
Exactwins overPrefix.
Because of this rule, rule order in the manifest does not matter. The /api and / entries in the shop Ingress from earlier chapters work for that reason. A request to /api/orders matches both, and the longer /api wins.
A worked example
This Ingress sends only the exact path /healthz to a health Service. It sends everything under /api to the API and everything else to the web Service.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop-paths
spec:
ingressClassName: example-class
rules:
- host: shop.example.com
http:
paths:
- path: /healthz
pathType: Exact
backend:
service:
name: health
port:
number: 80
- path: /api
pathType: Prefix
backend:
service:
name: api
port:
number: 80
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80With this manifest, /healthz goes to health. /healthz/ and /healthz/live fall through to web, because Exact does not match them and / matches everything. /api/v1/items goes to api.
ImplementationSpecific and portability
ImplementationSpecific hands the decision to the controller. Some controllers treat it like Prefix, some like Exact, and some interpret the value as a regular expression. Because behavior differs, the same manifest can route differently on two controllers. Use Exact or Prefix unless you need a controller feature, and read that controller's documentation first.
Common errors
- Using
Exactand expecting trailing-slash tolerance. Requests to/login/return 404 when only/loginis defined asExact. Switch toPrefixor add a second path. - Writing a regular expression under
Prefix. A path such as/user/[0-9]+is treated as literal elements and never matches. Regular expressions needImplementationSpecificand a controller that supports them. - Forgetting case sensitivity.
/Apidoes not match/api. - Expecting the backend to see a stripped path. Matching selects a backend only. The request path reaches the Service unchanged unless a controller-specific rewrite annotation changes it.
Exercise
An Ingress has two paths on the same host: /shop with Prefix pointing to Service a, and /shop/cart with Exact pointing to Service b. Which Service receives requests to /shop/cart, /shop/cart/, and /shopping?
Show answer
/shop/cart goes to b, since both paths match and the longer one wins (and Exact also beats Prefix on equal length). /shop/cart/ goes to a, because Exact does not match the trailing slash but the /shop prefix does. /shopping matches neither path, because shopping is not the element shop, so the default backend or a 404 handles it.