Kubernetes Ingress network deny some paths
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want to deny access to specific HTTP paths in Kubernetes, you need an L7 HTTP routing solution, not a pure network policy. This is the key architectural point. Kubernetes NetworkPolicy works at the IP and port level, so it cannot say “allow /api but deny /admin.” Path-based denial belongs in the ingress controller, an API gateway, or the application itself.
Why NetworkPolicy Cannot Deny Paths
NetworkPolicy controls traffic using concepts such as:
- pod selectors
- namespace selectors
- ingress and egress peers
- ports and protocols
It does not inspect HTTP request paths.
So this is possible with NetworkPolicy:
- allow traffic from namespace
frontendto service port80
This is not possible with NetworkPolicy:
- allow
/public - deny
/private
That distinction matters because many Kubernetes questions mix “network deny” and “URL path deny” as if they were the same thing.
The Usual Solution: Configure the Ingress Controller
If you are using an ingress controller such as NGINX Ingress, Traefik, or a cloud-managed gateway, path-based rules belong there.
One simple approach is to route sensitive paths differently. For example, with NGINX Ingress you can return a 403 for a specific path using a configuration snippet.
This is controller-specific, which is the important tradeoff. Path denial is possible, but the exact syntax depends on the ingress technology you chose.
A Cleaner Pattern: Route Denied Paths to a Blocking Backend
Sometimes a more maintainable option is to route blocked paths to a tiny service that always returns 403 or 404.
This keeps policy visible in plain routing configuration instead of embedding controller-specific logic snippets.
It is often easier for teams to reason about because the deny behavior is explicit.
Ingress vs Gateway vs Application Logic
There are three common layers where path denial can live:
- ingress controller annotations or custom snippets
- API gateway or service mesh HTTP policy
- application code itself
Choose based on ownership and complexity.
For example:
- if the path rule is infrastructure-wide, ingress or gateway is a good fit
- if the rule depends on user identity or business logic, application code is usually better
- if the cluster already uses a gateway API or service mesh, central policy may be cleaner than ingress annotations
The technical answer is not only “can it be done,” but also “which layer owns the rule.”
Status Code Choice Matters
Denying a path does not always mean returning 403.
Sometimes you may prefer:
- '
403 Forbiddenwhen the path exists but access is denied' - '
404 Not Foundwhen you do not want to reveal the endpoint' - redirect to another path when the route should be hidden behind a login flow
That is a product and security decision, not just a YAML decision.
Use Auth for Real Access Control
Path denial alone is often not enough. If the path should be available to some users but not others, use authentication or authorization instead of hard denial.
For example, an ingress controller may support:
- external auth integration
- basic auth
- JWT validation
- OAuth or OpenID Connect flows through a gateway
Static path blocking is best for paths that should be unavailable to everyone at that entry point.
Common Pitfalls
The biggest pitfall is trying to solve HTTP path restrictions with NetworkPolicy. It cannot see HTTP semantics.
Another issue is depending heavily on controller-specific annotations without documenting them. That can make migrations harder.
Teams also sometimes return 403 when 404 would be the safer information-disclosure choice.
Finally, do not confuse “deny this path for everyone” with “protect this path with authentication.” Those are different requirements.
Summary
- '
NetworkPolicycannot deny specific HTTP paths because it works below the HTTP layer.' - Path-based denial belongs in the ingress controller, gateway, or application logic.
- A controller-specific snippet or a dedicated deny backend are common implementation options.
- Choose
403,404, or redirect behavior deliberately. - Use authentication or authorization when the path should be selectively available rather than universally blocked.
Related reading
- Kubernetes ingress nginx redirect to https
- Kubernetes Ingress Path only works with /
- Kubernetes ingress rules How to use wildcard and specific subdomain together
- Kubernetes Ingress same with with master-slave architecture
- Kubernetes Ingress to External Service?
- Kubernetes Ingress vs. Service with externalIPs
- Kubernetes Network policy - deny all with allow all
- kubernetes PodSecurityPolicy set to runAsNonRoot, container has runAsNonRoot and image has non-numeric user appuser, cannot verify user is non-root

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.