Since it took me a while to figure things out, I thought sharing my solution might help
For the record, I had the same problem, I end up using:
- The plugin GitHub - alexashley/kong-plugin-path-prefix: A Kong plugin to rewrite the upstream request path to strip the path prefix
- The post-function plugin to set the
X-Forwarded-Prefixproperly
Full example with Kong Ingress Controller
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: strip-demo-2-path-prefix
config:
path_prefix: "/foo/"
plugin: path-prefix
---
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: set-forwarded-header
plugin: post-function
config:
access:
- |
ngx.var.upstream_x_forwarded_prefix='/foo'
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-2
annotations:
konghq.com/strip-path: "false"
konghq.com/plugins: strip-demo-2-path-prefix,set-forwarded-header
spec:
ingressClassName: kong
rules:
- http:
paths:
- backend:
service:
name: echo
port:
number: 80
path: /foo/bar
pathType: ImplementationSpecific
With this solution:
- Requests to
/foo/barare captured by Kong - They are routed to the upstream service to the path
/bar - The forwarded request has the header
X-Forwarded-Prefix: /foo(which makes the upstream service able to find out what its external url is)