Path-Based Routing to Microservices
Use Nginx location matching to route different URL paths to different backend microservices behind a single entry point.
Path-Based Routing to Microservices is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Door, Many Services
In a microservice system, clients should hit a single host while Nginx fans requests out to the right service based on the URL path.
This keeps clients simple and hides the internal topology.
The location Directive
Routing decisions live in location blocks. Each matches a path pattern and proxies to a specific upstream.
location /users/ {
proxy_pass http://users-service/;
}
location /orders/ {
proxy_pass http://orders-service/;
}Defining Upstreams
Name each service in an upstream block so locations stay readable and you can scale instances later.
upstream users-service {
server 10.0.0.1:8080;
}
upstream orders-service {
server 10.0.0.2:8080;
}Prefix vs Exact Match
A plain location /path/ is a prefix match. Use = for an exact match, which is faster and avoids accidental overlaps.
location = /health {
return 200 "ok";
}The Trailing Slash Rule
A trailing slash on proxy_pass changes path rewriting. With proxy_pass http://svc/; the matched prefix is stripped; without it the full path is forwarded.
location /users/ {
proxy_pass http://users-service/;
}
# /users/42 -> upstream sees /42Regex Locations
Prefix with ~ for case-sensitive regex matching, useful for routing by file type or dynamic segments.
location ~ ^/api/v[0-9]+/payments {
proxy_pass http://payments-service;
}Matching Priority
Nginx evaluates locations in a defined order: exact =, then literal prefixes, then regex in file order. Understanding this prevents surprising routes.
A Default Catch-All
Add a fallback location to serve a friendly response for unknown paths instead of leaking a default error.
location / {
return 404 "Unknown service";
}Shared Proxy Settings
Keep forwarding headers consistent across all services by including a shared snippet in each location.
location /users/ {
include /etc/nginx/proxy_headers.conf;
proxy_pass http://users-service/;
}Adding a New Service
Onboarding a service is just a new upstream plus a new location, no client changes required, which is the whole point of a gateway.
upstream search-service { server 10.0.0.3:8080; }
location /search/ {
proxy_pass http://search-service/;
}Testing the Routes
After reloading, curl each path and confirm it reaches the intended service.
curl http://localhost/users/42
curl http://localhost/orders/7Quick Check
With location /users/ { proxy_pass http://users-service/; }, what path does the upstream receive for a request to /users/42?
Recap
You built path-based routing to multiple services:
locationblocks map paths to upstreams- Trailing slash on
proxy_passcontrols prefix stripping - Match types: exact
=, prefix, and~regex - A catch-all handles unknown paths
One host now cleanly fronts an entire fleet of microservices.
Frequently asked questions
Is the “Path-Based Routing to Microservices” lesson free?
Yes — the full text of “Path-Based Routing to Microservices” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.
What will I learn in “Path-Based Routing to Microservices”?
Use Nginx location matching to route different URL paths to different backend microservices behind a single entry point. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Path-Based Routing to Microservices” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?
Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- API Versioning with Nginx
- Cross-Origin Resource Sharing (CORS)
- Rate Limiting & Throttling with Nginx
- Path-Based Routing to Microservices