Fix nginx location cannot be inside the exact location
Fix nginx location cannot be inside the exact location by moving the inner location out of the = block into a sibling position, then rerun nginx -t.
2026/11/27 23:28:46 [emerg] 12#12: location "/demo/child" cannot be inside the exact location "/demo" in /lab/before.conf:6nginx: [emerg] location "/demo/child" cannot be inside the exact location "/demo" in /lab/before.conf:6nginx: configuration file /lab/before.conf test failedA location that starts with =, such as location = /demo { … }, cannot contain another location. In the message, the first quoted path is the inner (child) location and the second is the outer (parent). Move the child out so it sits next to the parent inside the same server.
[1] Open the file and line at the end of the error (before.conf:6). That line is where the inner location starts. The container ran nginx -t -c /lab/before.conf. If the block came from an include, open the included file named in the error.
events {}http { server { listen 127.0.0.1:18080; location = /demo { location /demo/child { return 200 "child\n"; } } }}[2] Look at the modifier on the outer location, the location = /demo line just above line 6. = is an exact match: it applies only when the request path is exactly /demo. When nginx 1.28.0 reads a nested location and the parent is an exact match, it logs this error and stops reading the configuration. If the parent is a named location starting with @, a separate message appears: cannot be inside the named location.
[3] Move the inner location out and place it beside the parent in the same server. Keep in the exact-match block only the response for that one path. The child keeps its own path (/demo/child), so no path needs renaming.
events {}http { server { listen 127.0.0.1:18080; location = /demo { return 200 "demo\n"; } location /demo/child { return 200 "child\n"; } }}If the outer block held directives such as proxy_pass or root, the child is now outside them. Check whether the child needs the same directives.
[4] Check the fixed file. For comparison, /lab/prefix.conf (location /demo { location /demo/child { … } }, the parent without =) was run too.
$ nginx -t -c /lab/after.confnginx: the configuration file /lab/after.conf syntax is oknginx: configuration file /lab/after.conf test is successful$ nginx -t -c /lab/prefix.confnginx: the configuration file /lab/prefix.conf syntax is oknginx: configuration file /lab/prefix.conf test is successfulThe exit code was 1 before the fix and 0 for both files after. Dropping the = passes the check but changes the meaning: a prefix /demo matches every path that starts with /demo, including /demo2. If the configuration was meant to match exactly one path, split the blocks into siblings instead.
[5] After the check passes, reload the way your deployment does (nginx -s reload) and request both paths to see the responses. nginx -t only checks syntax. Which location serves a request is something to confirm with a real request.
If duplicate location appears after the move, two location blocks share one path. See Fix nginx duplicate location to find the two files. For errors about @ named locations, see nginx alias inside a named location.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.