Prompt
api routing by header and path
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
If you mean routing API traffic based on both the request path and a header, the usual setup is:
- Path-based routing:
/v1/users,/v2/users - Header-based routing: route only if a header matches, e.g.
X-Tenant: premiumorAccept: application/vnd.myapi.v2+json - Combined routing: both conditions must match
Common examples
1) NGINX
You can route using map + location:
map $http_x_api_version $backend {
default backend_v1;
"2" backend_v2;
}
server {
listen 80;
location /api/users {
proxy_pass http://$backend;
}
}
For more complex path + header logic, you can use if carefully or multiple location blocks.
2) Kong / API Gateway
Most API gateways support this directly with routes like:
- Path:
/api/users - Header:
X-API-Version: 2
Then forward to different upstream services.
3) Express / Node.js
You can do header + path checks in middleware:
app.use('/api/users', (req, res, next) => {
if (req.headers['x-api-version'] === '2') {
return v2Handler(req, res);
}
return v1Handler(req, res);
});
4) Spring Cloud Gateway
Route with predicates:
spring:
cloud:
gateway:
routes:
- id: users_v2
uri: http://users-v2
predicates:
- Path=/api/users/**
- Header=X-API-Version, 2
- id: users_v1
uri: http://users-v1
predicates:
- Path=/api/users/**
Pattern
Typically:
- Match path first
- Check header
- Send to matching backend
- Fallback if header missing or unknown
Example use cases
- API versioning
- A/B testing
- Canary releases
- Multi-tenant routing
- Feature flags
If you want, I can give you an example for a specific stack like NGINX, Kubernetes Ingress, AWS API Gateway, Kong, Spring Boot, or Express.