0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lezione

Routing basato sul path verso i microservizi

Utilizzi la corrispondenza delle location di Nginx per instradare percorsi URL diversi verso microservizi backend differenti dietro un unico punto di ingresso.

Routing basato sul path verso i microservizi è una lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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 /42

Regex 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/7

Quick 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:

  • location blocks map paths to upstreams
  • Trailing slash on proxy_pass controls prefix stripping
  • Match types: exact =, prefix, and ~ regex
  • A catch-all handles unknown paths

One host now cleanly fronts an entire fleet of microservices.

Domande Frequenti

La lezione «Routing basato sul path verso i microservizi» è gratuita?

Sì — il testo completo di «Routing basato sul path verso i microservizi» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), passa a CoddyKit PRO. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Cosa imparerò in «Routing basato sul path verso i microservizi»?

Utilizzi la corrispondenza delle location di Nginx per instradare percorsi URL diversi verso microservizi backend differenti dietro un unico punto di ingresso. Eserciti API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Non è richiesta alcuna esperienza precedente. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Routing basato sul path verso i microservizi»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Sì. Ogni lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Versionamento delle API con Nginx
  2. Cross-Origin Resource Sharing (CORS)
  3. Limitazione della frequenza e throttling con Nginx
  4. Routing basato sul path verso i microservizi
← Torna a API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)