GrepBoard/Manuals
605 manuals ·

Fix nginx location cannot be inside the exact location

·Tested on nginx 1.28.0 (Docker) · 2026-11·Test scope ↓·5 steps

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.

output
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 failed

A 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.

/lab/before.conf
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.

/lab/after.conf
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.

console
$ nginx -t -c /lab/after.conf
output
nginx: the configuration file /lab/after.conf syntax is oknginx: configuration file /lab/after.conf test is successful
console
$ nginx -t -c /lab/prefix.conf
output
nginx: the configuration file /lab/prefix.conf syntax is oknginx: configuration file /lab/prefix.conf test is successful

The 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.

#nginx#location#nginx -t#exact-match