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.
- Target user
- DevOps engineers debugging NGINX routing and 404/wrong-handler issues
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior NGINX engineer who understands location matching precedence cold. A request is hitting the wrong location block and I need it traced and fixed. I will provide: - All `location` blocks from the relevant `server` (paste them in file order) - The exact request URI that is misrouting and what it currently does vs what it should do - Any `try_files`, `rewrite`, `return`, or `alias`/`root` inside those blocks - The NGINX version Your job: 1. **Explain the match order** — state NGINX's actual precedence: exact `=`, then longest prefix `^~` (stops regex), then regex `~`/`~*` in file order, then plain prefix as fallback. Map my blocks onto this order. 2. **Trace the URI** — walk through which block my problem URI matches and exactly why, calling out the block that wins and the ones that are shadowed. 3. **Spot the trap** — identify common bugs: a greedy regex beating a prefix, a missing `^~`, `alias` vs `root` path confusion, or `try_files` falling through unexpectedly. 4. **Reorder/rewrite** — give corrected location blocks (and modifiers) so each URI routes correctly, preserving the others. 5. **Guard against regressions** — note which other URIs could be affected by the change and how to confirm they still route correctly. 6. **Verify** — provide curl commands per affected path and, where useful, an `error_log debug` or `add_header X-Debug-Location` technique to prove which block served the request. Output as: (a) precedence walkthrough for my URIs, (b) the bug, (c) corrected blocks, (d) verification matrix.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Write Safe NGINX Redirect & Rewrite Rules Prompt
Author correct, loop-free NGINX redirects and rewrites that preserve query strings, use the right status code, avoid open redirects, and prefer return over rewrite where possible, with test cases.
-
Secure NGINX Reverse-Proxy Vhost Prompt
Write a hardened NGINX reverse-proxy server block that forwards correct headers, hides upstream details, sets sane security headers, and avoids common proxy footguns like open host headers and missing real-IP handling.
-
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.
-
Debug NGINX 502/504 Upstream Errors Prompt
Diagnose why NGINX returns 502 Bad Gateway or 504 Gateway Timeout from an upstream by correlating the error log, the proxy block, and upstream health into a ranked root-cause list with fixes.
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.