Bloki location Nginx i dopasowywanie żądań
Opanuj bloki location Nginx oraz reguły dopasowywania, które decydują o tym, jaka konfiguracja obsłuży każde przychodzące żądanie.
Bloki location Nginx i dopasowywanie żądań to bezpłatna lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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
Często zadawane pytania
Czy lekcja „Bloki location Nginx i dopasowywanie żądań” jest bezpłatna?
Tak — pełny tekst „Bloki location Nginx i dopasowywanie żądań” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), przejdź na CoddyKit PRO. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.
Co nauczysz się w „Bloki location Nginx i dopasowywanie żądań”?
Opanuj bloki location Nginx oraz reguły dopasowywania, które decydują o tym, jaka konfiguracja obsłuży każde przychodzące żądanie. Ćwiczysz API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Nie wymagamy żadnego doświadczenia. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Bloki location Nginx i dopasowywanie żądań”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Tak. Każda lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Instalacja i konfiguracja Nginx
- Podstawowa konfiguracja Nginx
- Serwowanie treści statycznych za pomocą Nginx
- Bloki location Nginx i dopasowywanie żądań