Skip to content

Path prefixes

View as Markdown

Behind an Ingress or a Gateway, several apps often share one address, each under its own path. pathPrefix puts hello-kubernetes under a path such as /app/hello. rewritePath says which side removes that path: the proxy, or nobody.

rewritePath: false (recommended)
  1. Browser: asks for /app/hello/
  2. Ingress or Gateway: passes on /app/hello/
  3. App: serves at /app/hello/, and links its assets as /app/hello/css/main.css
rewritePath: true
  1. Browser: asks for /app/hello/
  2. Ingress or Gateway: strips the prefix, and passes on /
  3. App: serves at /, and links its assets as /app/hello/css/main.css

In both cases, the page links its stylesheet and images under the prefix, so the browser asks the proxy for them by the full path.

rewritePath The proxy Works with
false (recommended) passes the path on unchanged every Ingress controller and Gateway, with no annotations
true (the 1.x default) strips the prefix an HTTPRoute, which uses the standard URLRewrite filter. An Ingress needs your controller’s rewrite annotation.

Choose rewritePath=false unless you have a reason not to. The default changes to false in 2.0, so set it now and nothing changes for you then.

Terminal
helm install hello oci://ghcr.io/paulbouwer/charts/hello-kubernetes \
--set pathPrefix=/app/hello --set rewritePath=false \
--set ingress.enabled=true --set ingress.className=traefik \
--set service.type=ClusterIP

Leading and trailing slashes are optional: app/hello, /app/hello and /app/hello/ are the same prefix. The chart sets the Ingress or HTTPRoute path, the app’s own path and the probes’ path together.

For the full steps, see Behind an Ingress or Behind a Gateway. Without the chart, the app reads two settings: HANDLER_PATH_PREFIX, the path it serves under, and RENDER_PATH_PREFIX, the path its links use.

With your own proxy or Ingress, set pathPrefix and rewritePath, but leave ingress.enabled and httpRoute.enabled off. Point your proxy at the Service hello-kubernetes-<release name>, port 80.