Demystifying Nginx Location Directives
Nginx is the world's most popular web server and reverse proxy, powering millions of microservices, APIs, and web applications. However, configuring location blocks is notoriously confusing because Nginx does not simply match rules from top to bottom. It executes a nuanced priority evaluation combining exact equality, longest prefix matching, and sequential regular expressions.
The 4-Step Matching Precedence Rule
- Exact Match (
= /path): Nginx tests all exact match blocks first. If the URI matches identically, Nginx terminates search immediately and executes this block. - Preferential Prefix Match (
^~ /path): Nginx scans all prefix strings and identifies the longest matching prefix. If that block contains the^~modifier, Nginx skips regular expressions entirely and chooses this block. - Regular Expressions (
~and~*): If no exact or preferential match terminated the search, Nginx tests regular expressions in sequential order from top to bottom. The first matching regex wins. - Standard Longest Prefix Fallback: If none of the regular expressions match, Nginx falls back to the longest prefix block identified in Step 2.
Common Debugging Pitfalls
- Regex Order Conflict: Placing a broad regex like
location ~* \.png$above a specific asset directory may cause unexpected caching or header behavior. - The Trailing Slash Nuance: A request to
/apiwill not matchlocation /api/unless rewritten or redirected.
Frequently Asked Questions
What is the exact order of precedence for Nginx location directives?
Nginx follows a strict 4-step precedence algorithm: 1) Exact match '=' (stops immediately if matched). 2) Longest prefix search (if the longest matching prefix has '^~', regex search is completely skipped). 3) Regular expressions '~' and '~*' tested in sequential order from top to bottom (the first matching regex wins). 4) If no regex matches, the longest standard prefix from step 2 is selected.
What is the difference between ~ and ~* in Nginx?
Both modifiers denote regular expression matches. '~' enforces case-sensitive regex matching, whereas '~*' performs case-insensitive regex matching (commonly used for static assets like .(jpg|png|css|js)$).
Why did my regex location block lose to a prefix block?
If your prefix block uses the '^~' modifier (e.g. 'location ^~ /images/'), Nginx stops regex evaluation as soon as it determines this is the longest prefix match. To allow regex rules to match, remove the '^~' modifier.
Does the order of location blocks matter in an Nginx config file?
Yes, for regular expressions! Nginx stops at the FIRST matching regex block ('~' or '~*') from the top of the configuration file. For prefix blocks without modifiers, order does NOT matter because Nginx always evaluates all prefix blocks and selects the longest match.
How do I configure Nginx for a Single Page Application (SPA) like React or Vue?
Use 'location / { try_files $uri $uri/ /index.html; }'. This attempts to serve existing static files or directories, and falls back to index.html for client-side routing.
Is this Nginx location tester run on a server?
No. The entire Nginx location matching and regular expression simulation runs 100% client-side in your browser's JavaScript engine. Zero configuration data is sent to external servers.