Nginx Location Blocks & Request Matching
Master Nginx location blocks and the matching rules that decide which configuration handles each incoming request.
Nginx Location Blocks & Request Matching is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
Frequently asked questions
Is the “Nginx Location Blocks & Request Matching” lesson free?
Yes — the full text of “Nginx Location Blocks & Request Matching” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.
What will I learn in “Nginx Location Blocks & Request Matching”?
Master Nginx location blocks and the matching rules that decide which configuration handles each incoming request. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Nginx Location Blocks & Request Matching” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?
Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Nginx Installation & Setup
- Basic Nginx Configuration
- Serving Static Content with Nginx
- Nginx Location Blocks & Request Matching