RunOfflineTools Logo RunOfflineTools
🐧 Web Server Routing & DevOps

Nginx Location Matcher & Regex Tester

Paste your Nginx configuration, enter any request path, and visualize exactly which location block wins according to official Nginx precedence rules.

Test URLs:

βš™οΈ Nginx Location Directives

5 Block(s) Parsed
Matched Location Block

Line 7

Preferential Prefix (^~)
location ^~ /images/ {
    # Preferential prefix: stops regex inspection
    root /var/www/static;

Nginx Routing Order of Precedence

1. Exact Match =Stops Search
2. Preferential Prefix ^~Skips Regex
3. Regex Match ~ / ~*Top-to-Bottom
4. Standard Longest Prefix /Fallback

πŸ”Ž Step-by-Step Match Evaluation Log

Chronological trace of how Nginx evaluates this specific request URL:

ℹ️
Step 1: Exact Match Inspection (=):Testing if any '=' location matches '/images/banner.png' exactly.
β†ͺ️
Exact Check:Line 2: '/' does not match '/images/banner.png'.
ℹ️
Step 2: Longest Prefix Match Discovery:Scanning all prefix locations (^~ and standard prefix without modifiers).
⭐
Candidate Prefix:Line 7: '/images/' matches prefix (length 8).
⭐
Candidate Prefix:Line 23: '/' matches prefix (length 1).
πŸ†
Preferential Prefix Found (^~):Line 7: Longest prefix '/images/' has '^~' modifier. Regular expressions are skipped! Search terminates.

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

  1. Exact Match (= /path): Nginx tests all exact match blocks first. If the URI matches identically, Nginx terminates search immediately and executes this block.
  2. 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.
  3. 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.
  4. 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

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.