NGINX location & Regex Precedence Prompt
Figure out exactly which location block NGINX picks for a given request and fix the ordering bug that's routing your URL to the wrong handler — with the matching rules made explicit, not folklore.
- Target user
- Engineers debugging why a URL hits the wrong location, 404s, or skips a handler
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior NGINX engineer who knows the location matching algorithm cold: exact `=`, then prefix `^~`, then regex `~`/`~*` in file order, then the longest plain prefix as a fallback. You explain precedence by tracing a concrete request, not by quoting docs. I will provide: - The full set of `location` blocks in conflict: [PASTE LOCATION BLOCKS] - The request path(s) that go to the wrong place: [PASTE URLS] - What I expected each URL to match, and what it actually matches: [DESCRIBE EXPECTED VS ACTUAL] Do this: 1. **Trace the algorithm** for each problem URL: which modifiers exist (`=`, `^~`, `~`, `~*`, plain prefix), the order NGINX evaluates them, and the exact block that wins. State the rule that decided it. 2. **Explain the trap** if present: a regex block stealing traffic from a prefix because no `^~` short-circuits it; file-order dependence among regex blocks; a missing `=` for a hot exact path; or `try_files` inside the wrong block. 3. **Rewrite the blocks** so each URL lands where intended. Use `^~` to stop regex evaluation for a prefix, `=` for single hot paths (e.g. `/`), and order regex blocks deliberately since they match top-to-bottom. 4. **try_files / rewrite interaction** — if `try_files` or `rewrite` is involved, show how the rewritten URI re-enters location matching (or doesn't, with `last` vs `break`). 5. **Prove it** — give a small table mapping each test URL to the block it will match after the fix, and `curl` commands that exercise each path. Output: (a) a per-URL trace of the current (broken) match, (b) the corrected `location` blocks with comments on each modifier choice, (c) the URL→block proof table, (d) the curl test commands. Validate the corrected config with `nginx -t` and reload; do not hot-edit the live config.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Why this prompt works
NGINX location precedence is one of the most misremembered topics in web ops because the order you write blocks is not the order NGINX evaluates them — exact and ^~ prefix matches short-circuit before any regex runs, but plain prefixes are a fallback after regex. The prompt forces a per-URL trace through the real algorithm, so the answer explains why a block wins rather than just asserting it.
By naming the specific traps — a greedy regex stealing a prefix, file-order dependence among regex blocks, a missing = on a hot path — the model produces a fix targeted at your actual bug instead of a generic reordering.
The URL→block proof table plus curl commands make the result verifiable. You confirm every path lands where intended, and you gate the change behind nginx -t so a reorder that accidentally exposes a sensitive path never reaches production silently.
Related prompts
-
Apache .htaccess to NGINX Translation Prompt
Translate an Apache vhost or .htaccess (RewriteRules, auth, headers, directory rules) into a correct NGINX server block — preserving behavior and flagging the rules that have no clean NGINX equivalent.
-
Fix NGINX Location Block Precedence Prompt
Untangle why the wrong NGINX location block is matching a request by tracing prefix vs regex vs exact-match precedence, then reorder/rewrite the blocks so each URL hits the intended handler.
-
NGINX Canary & Blue-Green Routing Prompt
Design percentage-based canary and blue-green traffic routing in NGINX using split_clients and upstream weighting — with a sticky cohort, an instant rollback path, and a way to force yourself onto the canary for testing.
-
NGINX CORS & Preflight Proxy Prompt
Get correct, secure CORS handling in NGINX for a reverse-proxied API — OPTIONS preflight, Access-Control-* headers, credentials, and per-origin allow-listing — without the classic duplicate-header and wildcard-with-credentials footguns.
More NGINX prompts & error guides
Browse every NGINX prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.