Kong Ingress configuration for construting backend host based on request_uri

Following Nginx configuration construct proxy backend host URL based on request_uri

map $request_uri $target_backend {
    ~^/api/v2/(?<namespace>.+)/(?<customer>.+)/(?<product>.+)/$  api-v2-$customer-$product.$namespace.svc.cluster.local;

    ~^/api/v1/(?<namespace>.+)/(?<customer>.+)/(?<product>.+)/$  api-v1-$customer-$product.$namespace.svc.cluster.local;
}

server {
    listen 80 default_server;

    resolver kube-dns.kube-system.svc.cluster.local;

    location ~* ^/api/(.*)$ {
        proxy_pass http://$target_backend;
    }
}

How can I write this configuration for Kong Ingress Controller?

From docs, I can capture groups like Proxy Reference - v2.4.x | Kong - Open-Source API Management and Microservice Management but don’t find any example for constructing upstream host from it.

Kong doesn’t have native functionality to handle this sort of routing in configuration alone. You’ll want to write a custom plugin to extract content from the request and override the upstream host that’d normally be determined by the service.

The PDK provides a function to extract the request path, which you can pass through a regular expression with capture groups and build your new host. Once you have that, you can override the standard upstream host.


© 2019 Kong Inc.    Terms  •  Privacy  •  FAQ