Pfadbasiertes Routing zu Microservices
Verwenden Sie das Nginx-Location-Matching, um unterschiedliche URL-Pfade über einen einzigen Einstiegspunkt zu verschiedenen Backend-Microservices weiterzuleiten.
Pfadbasiertes Routing zu Microservices ist eine kostenlose API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Pfadbasiertes Routing zu Microservices“ kostenlos?
Ja — der vollständige Text von „Pfadbasiertes Routing zu Microservices“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Pfadbasiertes Routing zu Microservices“?
Verwenden Sie das Nginx-Location-Matching, um unterschiedliche URL-Pfade über einen einzigen Einstiegspunkt zu verschiedenen Backend-Microservices weiterzuleiten. Du übst API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zu starten?
Keine Vorkenntnisse erforderlich. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Pfadbasiertes Routing zu Microservices“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion Code schreiben und ausführen?
Ja. Jede API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- API-Versionierung mit Nginx
- Cross-Origin Resource Sharing (CORS)
- Rate Limiting und Throttling mit Nginx
- Pfadbasiertes Routing zu Microservices