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

Blocchi location di Nginx e corrispondenza delle richieste

Padroneggi i blocchi location di Nginx e le regole di corrispondenza che determinano quale configurazione gestisce ogni richiesta in ingresso.

Lezione 4 di 413 passaggi

Blocchi location di Nginx e corrispondenza delle richieste è 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.

Why Location Blocks Matter

A real site serves many paths differently: static files, an API, an admin area. Location blocks let Nginx apply different rules per URL path.

Anatomy of a Location

A location block matches a request URI and contains directives for it.

location /images/ {
  root /var/www;
}

Prefix Matching

By default a location is a prefix match: location /blog matches /blog, /blog/post, and anything starting with it.

location /blog {
  # matches /blog, /blog/, /blog/anything
}

Exact Match with =

The = modifier requires an exact URI. It is fast and great for hot paths like the health endpoint or homepage.

location = /health {
  return 200 'ok';
}

Regex Matching

Use ~ for case-sensitive and ~* for case-insensitive regular expressions, handy for file extensions.

location ~* \.(jpg|png|gif)$ {
  expires 30d;
}

The Matching Priority

Nginx does not just pick the first match. Order of precedence:

  • = exact match wins immediately
  • ^~ prefix stops regex search
  • regex (~/~*) in file order
  • longest plain prefix as fallback

The ^~ Modifier

^~ means: if this prefix is the longest match, use it and skip regex checking. Useful to protect a static directory from regex rules.

location ^~ /static/ {
  root /var/www;
}

Longest Prefix Wins

Among plain prefixes, the longest match is chosen, not the first listed. /api/v2 beats /api for a request to /api/v2/users.

Combining with proxy_pass

Location blocks commonly route to backends. Different paths can proxy to different upstreams.

location /api/ {
  proxy_pass http://127.0.0.1:8080;
}

Nested and Named Locations

Locations can nest inside others, and named locations (@name) are jumped to internally, often for fallbacks like try_files.

location / {
  try_files $uri @app;
}
location @app {
  proxy_pass http://backend;
}

Debugging Matches

When a request hits the wrong block, check: is there an exact or ^~ match stealing it? Is a regex matching unexpectedly? Reload and test with curl -v.

nginx -t && nginx -s reload

Quick Check

Test your location matching knowledge.

Recap

You learned Nginx location matching:

  • Prefix match is the default; = is exact
  • Regex uses ~ and ~*
  • Priority: exact, then ^~, then regex, then longest prefix
  • Longest prefix wins among plain matches
  • Named locations enable fallbacks; test with nginx -t and reload
Gratis per iniziare

Impara API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Blocchi location di Nginx e corrispondenza delle richieste» è gratuita?

Sì — il testo completo di «Blocchi location di Nginx e corrispondenza delle richieste» è 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 «Blocchi location di Nginx e corrispondenza delle richieste»?

Padroneggi i blocchi location di Nginx e le regole di corrispondenza che determinano quale configurazione gestisce ogni richiesta in 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 «Blocchi location di Nginx e corrispondenza delle richieste»?

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. Installazione e configurazione di Nginx
  2. Configurazione di base di Nginx
  3. Servire contenuti statici con Nginx
  4. Blocchi location di Nginx e corrispondenza delle richieste
← Torna a API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)