How to specify a prefix to a service exposed with an ingress
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To expose a service under a URL prefix with Kubernetes Ingress, you usually configure the Ingress path, not the Service itself. The Service still listens on its normal port, while the Ingress matches a path such as /api or /app and forwards matching traffic to that backend.
Use a Prefix Path in the Ingress Rule
The standard Ingress API supports prefix matching through pathType: Prefix. A basic example looks like this:
With this rule:
- '
/api' - '
/api/' - '
/api/users'
all route to api-service.
This is the normal answer when the application already knows how to serve content under /api.
Understand What Prefix Matching Means
Prefix does not rewrite the request path. It only decides whether the request should match the rule. If the incoming request is /api/users, the backend usually receives /api/users unless your ingress controller applies an explicit rewrite rule.
That distinction matters a lot:
- If the backend expects
/api/users, prefix routing alone is enough. - If the backend expects
/users, you also need a rewrite.
Rewriting the Prefix With NGINX Ingress
Path rewriting is controller-specific behavior, not generic Ingress behavior. With the NGINX Ingress Controller, a common pattern is:
Now a request for /api/users is forwarded as /users to the backend.
This is useful when your application serves from root but you want to publish it under a prefixed external URL.
Prefix Routing for Multiple Services
Ingress prefix rules are especially useful when one hostname serves multiple backends:
That is often easier to manage than giving every service its own hostname.
Service Configuration Does Not Hold the Prefix
A common misunderstanding is trying to put the prefix on the Service object. Kubernetes Services do not know about URL paths. They route TCP or UDP traffic to pods. HTTP path matching lives at the Ingress layer.
So the mental model is:
- Service handles network destination and port.
- Ingress handles hostnames, paths, and controller-specific rewrites.
Common Pitfalls
The biggest mistake is expecting pathType: Prefix to strip the prefix from the request. It only matches; it does not rewrite.
Another problem is mixing generic Ingress behavior with controller-specific annotations. A rewrite annotation that works with NGINX will not automatically work with every other ingress controller.
Developers also sometimes choose ImplementationSpecific when simple Prefix would be clearer. Use Prefix unless you actually need controller-specific regex behavior.
Finally, make sure the application itself is compatible with the published path. Some apps generate absolute links assuming they live at /, which breaks when they are exposed under /app without extra configuration.
Summary
- Put the URL prefix on the Ingress path, not on the Service.
- Use
pathType: Prefixwhen you want/apito match/api/.... - Prefix matching does not rewrite the path sent to the backend.
- Use controller-specific rewrite annotations only when the backend expects the prefix removed.
- Keep the division clear: Services route traffic, Ingress objects handle HTTP path rules.
Related reading
- How to specify master and worker nodes when using one machine to run Kubernetes?
- How to specify Proxy Pass in kubernetes
- How to specify static IP address for Kubernetes load balancer?
- How to SSH into a Kubernetes Node or Server
- How to specify all ports in Security group - CloudFormation
- How to specify api docs url for swagger ui in spring boot open api v3?
- How to SSH into a Kubernetes Node or Server
- How to SSH to docker container in kubernetes cluster?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.