Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for NGINX Difficulty: Intermediate ClaudeChatGPT

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

More NGINX prompts & error guides

Browse every NGINX prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.