Skip to content
Kong Logo | Kong Docs Logo
search
  • We're Hiring!
  • Docs
    • Kong Gateway
    • Kong Konnect
    • Kong Mesh
    • Plugin Hub
    • decK
    • Kubernetes Ingress Controller
    • Insomnia
    • Kuma

    • Docs contribution guidelines
  • Plugin Hub
  • Support
  • Community
  • Kong Academy
Get a Demo Start Free Trial
  • Kong Gateway
  • Kong Konnect
  • Kong Mesh
  • Plugin Hub
  • decK
  • Kubernetes Ingress Controller
  • Insomnia
  • Kuma

  • Docs contribution guidelines
  • 3.2.x (latest)
  • 3.1.x
  • 3.0.x
  • 2.8.x
  • 2.7.x
  • 2.6.x
  • Older Enterprise versions (2.1-2.5)
  • Older OSS versions (2.1-2.5)
  • Archive (pre-2.1)
    • Overview of Kong Gateway
      • Version Support Policy
      • Supported Installation Options
      • Supported Linux Distributions
    • Stability
    • Release Notes
      • Services
        • Overview
        • Configure Routes with Expressions
      • Upstreams
      • Plugins
      • Routing Traffic
      • Load Balancing
      • Health Checks and Circuit Breakers
      • Kong Performance Testing
    • Glossary
    • Get Kong
    • Services and Routes
    • Rate Limiting
    • Proxy Caching
    • Key Authentication
    • Load-Balancing
      • Overview
        • Overview
        • Deploy Kong Gateway in Hybrid mode
      • DB-less Deployment
      • Traditional
      • Overview
        • Helm
        • OpenShift with Helm
        • kubectl apply
        • Kubernetes Deployment Options
        • Using docker run
        • Build your own Docker images
        • Amazon Linux
        • Debian
        • Red Hat
        • Ubuntu
      • Running Kong as a non-root user
      • Securing the Admin API
      • Using systemd
      • Start Kong Gateway Securely
      • Programatically Creating Admins
      • Enabling RBAC
      • Overview
      • Download your License
      • Deploy Enterprise License
      • Using the License API
      • Monitor Licenses Usage
      • Default Ports
      • DNS Considerations
      • Network and Firewall
      • CP/DP Communication through a Forward Proxy
    • Kong Configuration File
    • Environment Variables
    • Serving a Website and APIs from Kong
      • Overview
      • Prometheus
      • StatsD
      • Datadog
      • Overview
      • Writing a Custom Trace Exporter
      • Tracing API Reference
    • Resource Sizing Guidelines
    • Security Update Process
    • Blue-Green Deployments
    • Canary Deployments
    • Clustering Reference
      • Log Reference
      • Dynamic log level updates
      • Customize Gateway Logs
      • Upgrade Kong Gateway 3.1.x
      • Migrate from OSS to Enterprise
    • Overview
      • Overview
      • Metrics
      • Analytics with InfluxDB
      • Analytics with Prometheus
      • Estimate Analytics Storage in PostgreSQL
      • Overview
      • Getting Started
      • Advanced Usage
        • Overview
        • Environment Variables
        • AWS Secrets Manager
        • Google Secrets Manager
        • Hashicorp Vault
        • Securing the Database with AWS Secrets Manager
      • Reference Format
      • Overview
      • Get Started with Dynamic Plugin Ordering
      • Overview
      • Enable the Dev Portal
      • Publish an OpenAPI Spec
      • Structure and File Types
      • Themes Files
      • Working with Templates
      • Using the Editor
        • Basic Auth
        • Key Auth
        • OIDC
        • Sessions
        • Adding Custom Registration Fields
        • Manage Developers
        • Developer Roles and Content Permissions
        • Authorization Provider Strategy
        • Enable Application Registration
        • Enable Key Authentication for Application Registration
          • External OAuth2 Support
          • Set up Okta and Kong for External Oauth
          • Set up Azure AD and Kong for External Authentication
        • Manage Applications
        • Theme Editing
        • Migrating Templates Between Workspaces
        • Markdown Rendering Module
        • Customizing Portal Emails
        • Adding and Using JavaScript Assets
        • Single Page App in Dev Portal
        • Alternate OpenAPI Renderer
      • SMTP
      • Workspaces
      • Helpers CLI
      • Portal API Documentation
    • Audit Logging
    • Keyring and Data Encryption
    • Workspaces
    • Consumer Groups
    • Event Hooks
    • FIPS 140-2
    • Overview
    • Enable Kong Manager
      • Services and Routes
      • Rate Limiting
      • Proxy Caching
      • Authentication with Consumers
      • Load Balancing
      • Overview
      • Create a Super Admin
      • Workspaces and Teams
      • Reset Passwords and RBAC Tokens
      • Basic Auth
        • Configure LDAP
        • LDAP Service Directory Mapping
        • Configure OIDC
        • OIDC Authenticated Group Mapping
      • Sessions
        • Overview
        • Enable RBAC
        • Add a Role and Permissions
        • Create a User
        • Create an Admin
    • Networking Configuration
    • Workspaces
    • Create Consumer Groups
    • Sending Email
    • Overview
    • File Structure
    • Implementing Custom Logic
    • Plugin Configuration
    • Accessing the Data Store
    • Storing Custom Entities
    • Caching Custom Entities
    • Extending the Admin API
    • Writing Tests
    • (un)Installing your Plugin
      • Overview
      • kong.client
      • kong.client.tls
      • kong.cluster
      • kong.ctx
      • kong.ip
      • kong.jwe
      • kong.log
      • kong.nginx
      • kong.node
      • kong.request
      • kong.response
      • kong.router
      • kong.service
      • kong.service.request
      • kong.service.response
      • kong.table
      • kong.tracing
      • kong.vault
      • kong.websocket.client
      • kong.websocket.upstream
      • Go
      • Javascript
      • Python
      • Running Plugins in Containers
      • External Plugin Performance
    • Overview
        • Overview
        • OpenID Connect with Curity
        • OpenID Connect with Azure AD
        • OpenID Connect with Google
        • OpenID Connect with Okta
        • OpenID Connect with Auth0
        • OpenID Connect with Cognito
      • Authentication Reference
      • Allow Multiple Authentication Plugins
    • Rate Limiting Plugin
      • Add a Body Value
    • GraphQL
      • gRPC Plugins
      • Configure a gRPC service
    • Overview
    • Information Routes
    • Health Routes
    • Tags
    • Debug Routes
    • Services
    • Routes
    • Consumers
    • Plugins
    • Certificates
    • CA Certificates
    • SNIs
    • Upstreams
    • Targets
    • Vaults
    • Keys
    • Licenses
    • Workspaces
    • RBAC
    • Admins
    • Developers
    • Consumer Groups
    • Event Hooks
    • Keyring and Data Encryption
    • Audit Logs
    • kong.conf
    • Injecting Nginx Directives
    • CLI
    • File Permissions Reference
    • Key Management
    • Performance Testing Framework
    • Router Expressions Language
    • FAQ

github-edit-pageEdit this page

report-issueReport an issue

enterprise-switcher-iconSwitch to OSS

On this page
  • Route and service interaction
  • How requests are routed
    • Priority points
    • Wildcard hosts
    • Header count
    • Regular expressions and prefix paths
  • Regular expressions
  • Dynamically rewrite request URLs with routes
  • Plugins for routes
  • Route configuration
Kong Gateway
3.1.x
  • Home
  • Kong Gateway
  • Key Concepts
  • Routes
  • Routes
You are browsing documentation for an outdated version. See the latest documentation here.

Routes

Routes determine how (and if) requests are sent to their services after they reach Kong Gateway. Where a service represents the backend API, a route defines what is exposed to clients.

A single service can have many routes. Once a route is matched, Kong Gateway proxies the request to its associated service.

Route and service interaction

Routes, in conjunction with services, let you expose your services to applications with Kong Gateway. Kong Gateway abstracts the service from the applications by using routes. Since the application always uses the route to make a request, changes to the services, like versioning, don’t impact how applications make the request. Routes also allow the same service to be used by multiple applications and apply different policies based on the route used.

For example, if you have an external application and an internal application that need to access the example_service service, but the external application should be limited in how often it can query the service to assure no denial of service. If a rate limit policy is configured for the service when the internal application calls the service, the internal application is limited as well. Routes can solve this problem.

In the example above, two routes can be created, say /external and /internal, and both routes can point to example_service. A policy can be configured to limit how often the /external route is used and the route can be communicated to the external client for use. When the external client tries to access the service via Kong Gateway using /external, they are rate limited. But when the internal client accesses the service using Kong Gateway using /internal, the internal client will not be limited.

How requests are routed

For each incoming request, Kong Gateway must determine which service gets to handle it based on the routes that are defined. With release 3.0, Kong Gateway introduced a new router that can be running in two modes, the traditional_compat mode, which is configured like prior releases, and the expressions mode which uses a new configuration scheme.

The default mode of the router is traditional_compat and the following sections describe how it is operates. For a description of the expressions mode, see How to Configure Routes using Expressions.

In general, the router orders all defined routes by their priority and uses the highest priority matching route to handle a request. If there are multiple matching routes with the same priority, it is not defined which of the matching routes will be used and Kong Gateway will use either of them according to how its internal data structures are organized.

In traditional_compat mode, the priority of a route is determined as follows, by the order of descending significance:

  1. Priority points
  2. Wildcard hosts
  3. Header count
  4. Regular expressions and prefix paths

Priority points

For the presence of each of a route’s methods, host, headers, and snis, a “priority point” will be added to the route. The number of “priority points” determines the overall order in which the routes will be considered. Routes with a higher “priority point” values will be considered before those with lower values. This means that if one route has methods defined, and second one has methods and headers defined, the second one will be considered before the first one.

Wildcard hosts

Among the routes with the same “priority point” value, those that have any wildcard host specification will be considered after routes that don’t have any wildcard host (or no host) specified.

Header count

The resulting groups are sorted so the routes with a higher number of specified headers have higher priority than those with a lower number of headers.

Regular expressions and prefix paths

Within the resulting groups of routes with equal priority, the router sorts the routes as follows:

  • Routes that have a regular expression path are considered first and are ordered by their regex_priority value. Routes with a higher regex_priority are considered before routes with lower regex_priority values.
  • Routes that have no regular expression path are ordered by the length of their paths. Routes with longer paths are considered before routes with shorter paths.

If one of the paths is a regular expression, the whole route is considered before routes that have no regular expressions. Similarly, if a route contains multiple prefix paths (and no regular expressions), its priority is determined by the longest of the paths.

Regular expressions

Regular expressions used in routes use more resources to evaluate than simple prefix routes. If many regular expressions must be evaluated to route a request, the latency introduced by Kong Gateway can suffer and its CPU usage can increase. In installations with thousands of routes, replacing regular expression routes by simple prefix routes can improve throughput and latency of Kong Gateway.

Starting with version 3.0, Kong Gateway uses the regular expression engine shipped with the Rust programming language if the router is operating in expressions or traditional_compatible mode. Prior versions used the PCRE library to evaluate regular expression. While the two engines are largely compatible, subtle differences exist between the two. Refer to the documentation pertinent to the engine that you are using if you have problems getting regular expression routes to work.

Dynamically rewrite request URLs with routes

Routes can be configured dynamically to rewrite the requested URL to a different URL for the upstream. For example, your legacy upstream endpoint may have a base URI like /api/old/. However, you want your publicly accessible API endpoint to now be named /new/api. To route the service’s upstream endpoint to the new URL, you could set up a service with the path /api/old/ and a route with the path /new/api.

Kong Gateway can also handle more complex URL rewriting cases by using regular expression capture groups in the route path and the Request Transformer Advanced plugin. For example, this can be used when you must replace /api/<function>/old with /new/api/<function>.

Kong Gateway 3.0.x or later ships with a new router. The new router can use regex expression capture groups to describe routes using a domain-specific language called Expressions. Expressions can describe routes or paths as patterns using regular expressions. For more information about how to configure the router using Expressions, see How to configure routes using expressions.

Plugins for routes

You can also use plugins to interface with routes. This allows you to further your routing capabilities in Kong Gateway.

See the following plugins for more information:

  • LDAP Authentication Advanced: Secure Kong Gateway clusters, routes, and services with username and password protection.
  • Mutual TLS Authentication: Secure routes and services with client certificate and mutual TLS authentication.
  • Route By Header: Route request based on request headers.
  • Route Transformer Advanced: Transform routing by changing the upstream server, port, or path.

Route configuration

Before you can start making requests against a service, you must add a route to it.

You can add routes to a service in Kong Gateway using the following methods:

  • Send an HTTP request using the Admin API
  • Create a route using the Kong Manager user interface
Thank you for your feedback.
Was this page useful?
  • Kong
    THE CLOUD CONNECTIVITY COMPANY

    Kong powers reliable digital connections across APIs, hybrid and multi-cloud environments.

    • Company
    • Customers
    • Events
    • Investors
    • Careers Hiring!
    • Partners
    • Press
    • Contact
  • Products
    • Kong Konnect
    • Kong Gateway
    • Kong Mesh
    • Get Started
    • Pricing
  • Resources
    • eBooks
    • Webinars
    • Briefs
    • Blog
    • API Gateway
    • Microservices
  • Open Source
    • Install Kong Gateway
    • Kong Community
    • Kubernetes Ingress
    • Kuma
    • Insomnia
  • Solutions
    • Decentralize
    • Secure & Govern
    • Create a Dev Platform
    • API Gateway
    • Kubernetes
    • Service Mesh
Star
  • Terms•Privacy
© Kong Inc. 2023