For each incoming request, Kong Gateway must determine which Gateway Service will handle it based on the Routes that are defined.
The Kong Gateway router orders all defined Routes by their priority and uses the highest priority matching Route to proxy the request.
To maximise performance, the Kong Gateway router orders all defined Routes by their priority and uses the highest priority matching Route to handle a request. How Routes are prioritized depends on the router mode you’re using.
For more information, see the detailed expressions or traditional sections.
The Route entity allows you to configure proxy behavior on a per route basis by setting the strip_path
, preserve_host
, and path_handling
values.
In most cases, strip_path
and preserve_host
should be false
(this is the default value), and path_handling
should be set to v0
.
It may be desirable to specify a path prefix to match a Route, but not
include it in the upstream request. To do so, use the strip_path
boolean
property by configuring a Route like so:
{
"paths": ["/service"],
"strip_path": true,
"service": {
"id": "..."
}
}
Enabling this flag instructs Kong Gateway that when matching this Route, and proceeding
with the proxying to a service, it should not include the matched part of
the URL path in the upstream request’s URL. For example, the following
client’s request to the above Route:
GET /service/path/to/resource HTTP/1.1
Host: ...
This causes Kong Gateway to send the following upstream request:
GET /path/to/resource HTTP/1.1
Host: ...
The same way, if a regex path is defined on a Route that has strip_path
enabled, the entirety of the request URL matching sequence will be stripped.
For example:
{
"paths": ["/version/\d+/service"],
"strip_path": true,
"service": {
"id": "..."
}
}
The following HTTP request matching the provided regex path:
GET /version/1/service/path/to/resource HTTP/1.1
Host: ...
Is proxied upstream by Kong Gateway as:
GET /path/to/resource HTTP/1.1
Host: ...
When proxying, Kong Gateway’s default behavior is to set the upstream request’s Host
header to the hostname specified in the Gateway Service’s host
. The
preserve_host
field accepts a boolean flag instructing Kong Gateway not to do so.
For example, when the preserve_host
property is not changed and a Route is
configured like so:
{
"hosts": ["service.com"],
"service": {
"id": "..."
}
}
A possible request from a client to Kong Gateway could be:
GET / HTTP/1.1
Host: service.com
Kong Gateway would extract the Host header value from the Service’s host
property,
and would send the following upstream request:
GET / HTTP/1.1
Host: <my-service-host.com>
However, by explicitly configuring a Route with preserve_host=true
:
{
"hosts": ["service.com"],
"preserve_host": true,
"service": {
"id": "..."
}
}
And assuming the same request from the client:
GET / HTTP/1.1
Host: service.com
Kong Gateway would preserve the Host on the client request and would send the following
upstream request instead:
GET / HTTP/1.1
Host: service.com
The path_handling
parameter accepts v0
or v1
.
-
v0
is the behavior used in Kong 0.x, 2.x, and 3.x. It treats service.path
, route.path
and request path as segments of a URL. It will always join them via slashes. Given a Service path /s
, Route path /r
and request path /re
, the concatenated path will be /s/re
. If the resulting path is a single slash, no further transformation is done to it. If it’s longer, then the trailing slash is removed.
-
v1
is the behavior used in Kong 1.x. It treats service.path
as a prefix, and ignores the initial slashes of the request and Route paths. Given Service path /s
, Route path /r
and request path /re
, the concatenated path will be /sre
.
path_handling
v1 is not supported in the expressions
router and may be removed in a future version of Kong Gateway. We strongly recommend using v0
.
Both versions of the algorithm detect “double slashes” when combining paths, replacing them by single
slashes.
Expand this block to see a table showing detailed v0
and v1
examples
service.path |
route.path |
request |
route.strip_path |
route.path_handling |
request path |
upstream path |
/s |
/fv0 |
req |
false |
v0 |
/fv0/req |
/s/fv0/req |
/s |
/fv0 |
blank |
false |
v0 |
/fv0 |
/s/fv0 |
/s |
/fv1 |
req |
false |
v1 |
/fv1/req |
/sfv1/req |
/s |
/fv1 |
blank |
false |
v1 |
/fv1 |
/sfv1 |
/s |
/tv0 |
req |
true |
v0 |
/tv0/req |
/s/req |
/s |
/tv0 |
blank |
true |
v0 |
/tv0 |
/s |
/s |
/tv1 |
req |
true |
v1 |
/tv1/req |
/s/req |
/s |
/tv1 |
blank |
true |
v1 |
/tv1 |
/s |
/s |
/fv0/ |
req |
false |
v0 |
/fv0/req |
/s/fv0/req |
/s |
/fv0/ |
blank |
false |
v0 |
/fv0/ |
/s/fv01/ |
/s |
/fv1/ |
req |
false |
v1 |
/fv1/req |
/sfv1/req |
/s |
/fv1/ |
blank |
false |
v1 |
/fv1/ |
/sfv1/ |
/s |
/tv0/ |
req |
true |
v0 |
/tv0/req |
/s/req |
/s |
/tv0/ |
blank |
true |
v0 |
/tv0/ |
/s/ |
/s |
/tv1/ |
req |
true |
v1 |
/tv1/req |
/sreq |
/s |
/tv1/ |
blank |
true |
v1 |
/tv1/ |
/s |
You can use the following recommendations to increase routing performance:
- In
expressions
mode, we recommend putting more likely matched Routes before (as in, higher priority) those that are less frequently matched.
- Regular expressions in Routes use more resources to evaluate than simple prefixes. In installations with thousands of Routes, replacing a regular expression with simple prefix can improve throughput and latency of Kong Gateway. If a regex must be used because an exact path match must be performed, using the expressions router will significantly improve Kong Gateway’s performance in this case.