🔀

NGINX LOCATION ROUTING & PROXY PASS SIMULATOR

Which location block wins and what proxy_pass sends upstream, worked out the way nginx does it

as a browser sends it; the result updates as you type

Location blocks 0

in the order they are in the config

Result

⬆ Import an nginx config

Comments, quotes and nested braces are read as nginx reads them. Nested locations, if { }, named locations and includes are listed and left out.

⚡ Test many paths at once

Each is matched against the blocks above.

📋 Export as an nginx config

, quoted where nginx needs it. Check it with sudo nginx -t before reloading.

How nginx picks a location
  1. The path is cleaned up first. %XX codes are decoded (so %2F becomes a real /), . and .. are resolved, repeated slashes are merged, and the query string is set aside. Matching uses this path; a broken %XX code or a .. above the top gets 400 Bad Request.
  2. = exact: a block whose pattern is the whole path wins at once.
  3. Trailing slash: if a prefix block ending in / passes requests on (proxy_pass and the like) and the path is that pattern without the /, nginx answers 301 to add it, without checking regexes. A block whose pattern is exactly the path stops this.
  4. Longest prefix: of the plain and ^~ prefixes that match, nginx remembers the longest. If it has ^~, it wins and regexes aren’t checked.
  5. Regexes (~, and ~* for any case) are tried in the order they appear; the first that matches wins.
  6. Otherwise the remembered longest prefix wins. With no match at all, nginx serves files from the server’s root.
What proxy_pass sends: without a path (http://backend:3000), the request goes on exactly as the browser sent it. With a path, even just /, nginx replaces the matched prefix with that path, using the cleaned-up path, and %-encodes it again if the request had %XX codes. In a regex block proxy_pass can’t have a path: nginx -t refuses it.
Share MySysAd

⚠️ IMPORTANT DISCLAIMER: The tools and scripts on this site are provided "AS IS" for educational purposes.

Review destructive commands before executing. By using this site, you assume all risk and agree to our Full Terms of Service.

Which location wins, and what reaches the backend?

Nginx picks a location block by rules that trip up even experienced admins, and a single slash in proxy_pass changes the URL your app receives. Enter a request path and your location blocks, or paste your config, and the simulator shows which block wins and the exact request line the backend gets. It cleans up the path as nginx does, knows the trailing-slash redirect, and flags blocks nginx -t would refuse; it was checked against real nginx with thousands of requests.

Common questions

What does the trailing slash in proxy_pass do?

With a URI part, nginx replaces the matched location prefix: location /api/ { proxy_pass http://app:8080/; } sends /api/users as /users. Without one, the original path passes unchanged: proxy_pass http://app:8080; sends /api/users as is. Mismatched slashes are the usual cause of 404s and double slashes at the backend.

How does nginx choose a location block?

An exact match location = /path wins first. Otherwise nginx finds the longest matching prefix; if that prefix uses ^~ it’s used straight away. If not, regex locations (~ and ~*) are tried in the order they appear, and the first match wins. Only when no regex matches is the longest prefix used.

Why does my regex location override a prefix location?

Because regexes are checked after prefix matching and win if they match, even when a longer prefix also matched. Add ^~ to the prefix location (location ^~ /static/) to stop nginx checking regexes for those paths.

How do I strip a path prefix before proxying?

Use a matching slash on both sides: location /app/ { proxy_pass http://backend/; } turns /app/login into /login. Inside regex locations proxy_pass can’t replace the path that way; use rewrite ^/app/(.*)$ /$1 break; instead.

Why does nginx redirect /api to /api/?

When a prefix location ends in a slash and passes requests on (location /api/ { proxy_pass http://app; }), nginx answers a request for /api with 301 to /api/, before it looks at any regex location. Add location = /api { … } if you want /api handled differently.

Does nginx match the URL as typed or decoded?

Decoded and cleaned up: %XX codes are decoded, . and .. are resolved and repeated slashes are merged before matching, so //api/x and /static/../api/x match location /api/. When proxy_pass has a path, that cleaned-up path is what the backend gets (with %2F turned into a real slash); without one, the request goes on exactly as the browser sent it.

How do I test an nginx config on the server?

sudo nginx -t checks the syntax and sudo nginx -T prints the full config with all includes. Reload with sudo systemctl reload nginx so running connections aren’t dropped.

Related tools: Permission Simulator (403 errors) · Log Parser · Security Header Tester · All tools A–Z