/v1/users
and /v2/users
, while still matching certain common paths or endpoints./project
and /project-management
) you will want to ensure that the correct path is matched to requests.protocol
is the RFC’s scheme
, host
is the authority
and listenPath/endpointPath
is the path
.
The listen path is a mandatory field defined in the API definition loaded onto the Gateway. Tyk will compare the incoming request path (after stripping off the host name) against all registered listen paths to identify the API definition (and hence Gateway configuration) that should be used to handle the request. If no match is found, then Tyk will reject the request with HTTP 404 Not Found
.
If a match is found, Tyk will then handle the request according to the configuration in the matching API definition. The endpoint path will be compared against any endpoints defined in the API definition to determine which endpoint-level configuration (such as transformation middleware) should be applied to the request.
true
then Tyk will perform an exact match of the request against the configured listen path including the trailing /
used to mark the end of the listen path. For example:
/app
will match requests to /app
, /app/
and /app/*
but will not match /app1/*
or /apple/
/app
<gateway-address>/<listen-path>/<version-identifier>/<endpoint-path>
will be proxied to <target-url>/<listen-path>/<version-identifier>/<endpoint-path>
./
). When performing a match against endpoints configured in the API definition, Tyk treats the configured patterns as regular expressions, allowing advanced users to perform complex endpoint path matching by use of regexes in their API definitions.
HTTP 404
if no match is found).
The APIs loaded on the Gateway are ordered as follows:
/api/{category}/user
will rank higher than /api/123/user
/{id}
will be replaced with /
/
) from most to least - e.g. /api/user/profile
before /api/user
/api/user
before /api/{userId}
/api/user-access
before /api/user
/api/aba
before /api/abc
/users
.
/users/{id}
would match any URL of the form /users/123
, where 123
is treated as a dynamic segment.
Tyk converts dynamic path segments in the configured pattern into capturing groups in the regular expression (([^/]+)
)as follows (note that these examples include the ^
and $
that are added when exact matching is in use):
Path pattern | Regular Expression used in match | |
---|---|---|
1 | /users/{id} | ^/users/([^/]+)$ |
2 | /static/{path}/assets/{file} | ^/static/([^/]+)/assets/[^/]+)$ |
3 | /orders/{orderId}/items/{itemId} | ^/orders/([^/]+)/items/([^/]+)$ |
4 | /orders/{orderId}/items/{itemId} | ^/orders/([^/]+)/items/([^/]+)$ |
/users/123
, where id
is dynamic./static/images/assets/logo.png
, where path
and file
are dynamic./orders/456/items/789
, where orderId
and itemId
are dynamic./orders/456/items/789
, where orderId
and itemId
are dynamic.*
(wildcard) (or {*}
in older versions) to take advantage of unnamed parameters, for example: taking example (3) from above, you can define the pattern as /orders/*/items/*
and Tyk will interpret this as the same regular expression (^/orders/([^/]+)/items/([^/]+)$
).
^
and $
that are added when exact matching is in use):
Listen path pattern | Regular Expression used in match | |
---|---|---|
1 | /users/{id}/profile/{type:[a-zA-Z]+} | ^/users/([^/]+)/profile/([a-zA-Z]+)$ |
2 | /items/{itemID:[0-9]+}/details/{detail} | ^/items/([0-9]+)/details/([^/]+)$ |
3 | /products/{productId}/reviews/{rating:\d+} | ^/products/([^/]+)/reviews/(\d+)$ |
id
is dynamic, and type
only includes alphabetic characters./items/45/details/overview
, where itemID
is a number and detail
is dynamic./products/987/reviews/5
, where productId
is dynamic and rating
must be a digit.^
and $
that are added when exact matching is in use):
User Input | Converted Regular Expression | |
---|---|---|
1 | /users/{id}/profile/{type:[a-zA-Z]+} | ^/users/([^/]+)/profile/([^/]+)$ |
2 | /items/{itemID:[0-9]+}/details/{detail} | ^/items/([^/]+)/details/([^/]+)$ |
3 | /products/{productId}/reviews/{rating:\d+} | ^/products/([^/]+)/reviews/([^/]+)$ |
^/users/(?i)[0-7][0-9A-HJKMNP-TV-Z]{25}$
(?i)
makes the particular matching case-insensitivetyk.conf
) or using the equivalent environment variables, as described below.
The default behavior of Tyk Gateway is wildcard matching as both prefix and suffix controls default to false
; the introduction of these controls does not change the behavior of any existing API definitions unless either is set to true
to enforce a different path matching mode.
Tyk recommends the use of exact path matching with strict routes for most use cases
true
, Tyk will perform wildcard matching. In this mode, Tyk will attempt to match the pattern with any part of the URL path.
For example, the pattern /user
will match all of the following URLs:
/my-api/user
/my-api/users
/my-api/v2/user/12345
/my-api/groups/12/username/abc
/json
will only match request URLs that begin with /json
, rather than matching any URL containing /json
.
The gateway checks the request URL against several variations depending on whether path versioning is enabled:
/listen-path/v4/json
/listen-path/json
/json
^
) if the URL begins with a /
, to ensure that the URL begins with the specified pattern. For example, /json
would be evaluated as ^/json
.
For patterns that already start with ^
, the gateway will already perform prefix matching so TYK_GW_HTTPSERVEROPTIONS_ENABLEPATHPREFIXMATCHING
will have no impact.
This option allows for more specific and controlled routing of API requests, potentially reducing unintended matches. Note that you may need to adjust existing route definitions when enabling this option.
Example:
/json
might match /api/v1/data/json
./json
would not match /api/v1/data/json
, but would match /json/data
./json
will only match request URLs that end with /json
, rather than matching any URL containing /json
.
The gateway checks the request URL against several variations depending on whether path versioning is enabled:
/listen-path/v4/json
/listen-path/json
/json
$
), to ensure that the URL ends with the specified pattern. For example, /json
would be evaluated as /json$
.
For patterns that already end with $
, the gateway will already perform suffix matching so TYK_GW_HTTPSERVEROPTIONS_ENABLEPATHSUFFIXMATCHING
will have no impact.
This option allows for more specific and controlled routing of API requests, potentially reducing unintended matches. Note that you may need to adjust existing route definitions when enabling this option.
Example:
/json
might match /api/v1/json/data
./json
would not match /api/v1/json/data
, but would match /data/json
./json
being treated as ^/json$
, ensuring the URL exactly matches /json
with no additional characters before or after it. This allows matching against any of the matching paths explicitly:
/listen-path/v4/json
- targeting API by listen path and version/v4/json
- only targeting the version for any API/json
- only targeting the endpoint^
and $
) or by omission of the /
prefix from the pattern as follows:
^
at the start of the listen path/endpoint definition the Gateway will apply prefix matching when checking against this pattern$
at the end of the listen path/endpoint definition the Gateway will apply prefix matching when checking against this pattern/
from the listen path/endpoint definition then prefix matching will not be applied even if set in the Gateway config^/
and ends with a $
^
and $
symbols in your listen path and endpoint path patterns will interact with the configured matching mode.
Prefix mode | Suffix mode | Pattern | Effective matching mode |
---|---|---|---|
❌️ | ❌️ | /my-api/my-endpoint/{my-param} | wildcard |
✅ | ❌️ | /my-api/my-endpoint/{my-param} | prefix |
❌️ | ✅ | /my-api/my-endpoint/{my-param} | suffix |
✅ | ✅ | /my-api/my-endpoint/{my-param} | exact |
❌️ | ❌️ | ^/my-api/my-endpoint/{my-param} | prefix |
✅ | ❌️ | ^/my-api/my-endpoint/{my-param} | prefix |
❌️ | ✅ | ^/my-api/my-endpoint/{my-param} | exact |
✅ | ✅ | ^/my-api/my-endpoint/{my-param} | exact |
❌️ | ❌️ | /my-api/my-endpoint/{my-param}$ | suffix |
✅ | ❌️ | /my-api/my-endpoint/{my-param}$ | exact |
❌️ | ✅ | /my-api/my-endpoint/{my-param}$ | suffix |
✅ | ❌️ | /my-api/my-endpoint/{my-param}$ | exact |
❌️ | ❌️ | ^/my-api/my-endpoint/{my-param}$ | exact |
✅ | ❌️ | ^/my-api/my-endpoint/{my-param}$ | exact |
❌️ | ✅ | ^/my-api/my-endpoint/{my-param}$ | exact |
✅ | ✅ | ^/my-api/my-endpoint/{my-param}$ | exact |
❌️ | ❌️ | my-api/my-endpoint/{my-param} | wildcard |
✅ | ❌️ | my-api/my-endpoint/{my-param} | wildcard |
❌️ | ✅ | my-api/my-endpoint/{my-param} | suffix |
✅ | ✅ | my-api/my-endpoint/{my-param} | suffix |
❌️ | ❌️ | /my-api/my-endpoint/* | wildcard |
✅ | ❌️ | /my-api/my-endpoint/* | prefix |
❌️ | ✅ | /my-api/my-endpoint/* | wildcard |
✅ | ✅ | /my-api/my-endpoint/* | prefix |
❌️ | ❌️ | my-api/my-endpoint/* | wildcard |
✅ | ❌️ | my-api/my-endpoint/* | wildcard |
❌️ | ✅ | my-api/my-endpoint/* | wildcard |
✅ | ✅ | my-api/my-endpoint/* | wildcard |
/listen-path/v4/json
in wildcard mode. To achieve prefix or
suffix matching in older versions, consider that the input is a regular
expression. Depending on your setup, you could use [^/]+
for any of
the path segments or finer grained regular expressions.
^/[^/]+/v4/json$
- exact match for any listen path, /v4/json
below^/{listenPath}/{version}/json$
, exact match with named parameters^/{*}/json
, prefix match (omits the $
).^/listen-path/users
. You might consider
using the ending $
expression to achieve exact matching.