Nginx-Location-Blöcke & Anfragezuordnung
Beherrschen Sie Nginx-Location-Blöcke und die Zuordnungsregeln, die bestimmen, welche Konfiguration jede eingehende Anfrage verarbeitet.
Nginx-Location-Blöcke & Anfragezuordnung 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.
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 reloadQuick 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 -tand reload
Häufig gestellte Fragen
Ist die Lektion „Nginx-Location-Blöcke & Anfragezuordnung“ kostenlos?
Ja — der vollständige Text von „Nginx-Location-Blöcke & Anfragezuordnung“ 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 „Nginx-Location-Blöcke & Anfragezuordnung“?
Beherrschen Sie Nginx-Location-Blöcke und die Zuordnungsregeln, die bestimmen, welche Konfiguration jede eingehende Anfrage verarbeitet. 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 „Nginx-Location-Blöcke & Anfragezuordnung“?
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
- Nginx installieren und einrichten
- Grundlegende Nginx-Konfiguration
- Statische Inhalte mit Nginx bereitstellen
- Nginx-Location-Blöcke & Anfragezuordnung