iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
The Nginx “duplicate location” error means the effective configuration contains colliding location declarations in the same server context. The declarations may be in one file, in two enabled site files, or in fragments pulled in more than once through nested include directives.
Run nginx -t, note the exact file and line number, trace the active include tree, and remove or deliberately merge the conflicting blocks. Do not simply delete whichever block appears first: location modifiers, URI specificity, directive inheritance, and regular-expression order can change how requests are routed.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallWhat the duplicate-location error actually means
Nginx builds one effective configuration from the main configuration file and every file loaded by its include directives. A server context can contain one or more location contexts that define how particular URI sets are processed. If two declarations collide in that effective server configuration, parsing can stop with an error similar to:
nginx: [emerg] duplicate location "/api/" in /etc/nginx/sites-enabled/app.conf:42
nginx: configuration file /etc/nginx/nginx.conf test failed
The path, line number, and URI in the actual message are the most valuable clues. The two declarations do not have to be adjacent or even in the same physical file. A common arrangement is a hand-edited block in sites-enabled plus a second copy in conf.d, or one fragment included directly and again through another fragment.
#1 Best Overall
Typical causes include:
- Copying a complete
locationblock while adapting an application configuration. - Enabling two site fragments that define the same URI and modifier.
- Including the same file through two paths, often because a wildcard and an explicit filename both match it.
- Following a symlink as though it were a separate configuration file when the target is already included elsewhere.
- Putting two application blocks in one
servereven though they were intended for separate virtual hosts.
The parser reports the collision, not the reason it happened. You must inspect the complete configuration tree to identify the other declaration.
Read the complete error before changing anything
Start with a configuration test and capture all of its output:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
sudo nginx -t
Record:
- The URI shown after “duplicate location”.
- The location modifier, if the message displays one, such as
=or~. - The reported filename.
- The line number.
- Whether Nginx reports additional errors after the duplicate.
Fix the first reported error before investigating later messages. A parser can stop at the first invalid declaration, so subsequent diagnostics may be consequences rather than independent problems.
Do not reload a configuration that has failed the test. The currently running worker processes normally continue using the last valid configuration, but an attempted restart can fail and leave a service unavailable if the invalid file replaces the only known-good configuration.
Confirm which configuration tree Nginx is using
Administrators often inspect /etc/nginx/nginx.conf while the installed binary is using a different compiled default path, a custom prefix, or a distribution-specific layout. Check the binary’s build options:
nginx -V
Look for the --conf-path= option in the output. Nginx documents this option as the way to verify the compiled default configuration path. If the command is unavailable without elevated privileges, run it with the same account and environment used by the service manager, or use sudo nginx -V.
Recommended Free Tools
The path from --conf-path is the starting point. Open that main file and identify every include. Common patterns include:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
The exact directories differ by operating system and packaging. Treat every include as part of the active configuration until you establish that it is outside the loaded server context or does not match any file.
Rank #2
Trace includes and locate both declarations
Follow the include tree
Read the main file from top to bottom. For each include, open matching files and repeat the process for nested includes. Keep a short list of the files you have visited. This catches a subtle but common failure mode: a fragment includes another file, while the parent configuration also includes that same file directly.
Pay particular attention to wildcard patterns. A file can be read twice when an explicit include names it and a wildcard also matches it. Symlinks can produce the same effect when both a link and the target are selected by a pattern. Keep one canonical copy and remove the duplicate include or disable the redundant link.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsSearch for the URI and modifier
Once you know the URI from the error, search all directories covered by the include tree. For example, from the configuration root:
sudo grep -RIn --include='*.conf' 'location' /etc/nginx
To narrow the result to a particular URI, search for its literal text:
sudo grep -RIn --include='*.conf' 'location /api/' /etc/nginx
Adjust the root and filename pattern to match your installation. Search for the exact modifier as well as the URI. These are different declarations:
location /health { ... }
location = /health { ... }
location ^~ /health/ { ... }
location ~ .php$ { ... }
Also inspect files that do not use a .conf suffix if the include pattern permits them. A search limited to one extension can miss an enabled site file.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Check whether the same file is included twice
If you find only one textual copy of the block, return to the include tree. Look for:
- The same filename named explicitly and matched by a wildcard.
- A symlink in an enabled directory whose target is also present as a regular file.
- A fragment included by two parent fragments.
- Two virtual-host files that are both loaded into one
servercontext through copy-and-paste or an unclosed brace.
Count declarations in the effective context, not merely in the directory listing. A file that exists on disk but is not matched by an include is irrelevant to this error; a file reached through two include paths is not.
Rank #3
Understand Nginx location selection before merging anything
A location’s URI and modifier determine more than whether two lines look similar. Nginx applies a defined selection process:
| Location form | Selection behavior | Why it matters during a repair |
|---|---|---|
location = /path |
Exact match; searching stops immediately. | Combining it with a prefix block can change which requests reach the exact handler. |
location ^~ /path/ |
Longest matching prefix is selected and regular-expression checks are suppressed for that prefix. | Removing ^~ can send requests to a regex block that previously could never win. |
location /path/ |
Prefix matching; the longest matching prefix is considered before regex evaluation. | Two copies with the same prefix must be consolidated; a shorter or longer prefix is not equivalent. |
location ~ pattern or ~* |
Regular-expression locations are checked in configuration order; the first matching expression wins. | Moving or merging regex blocks can change routing even when their patterns are unchanged. |
An exact match has priority over other candidates and terminates the search. For prefixes, Nginx chooses the longest match. A ^~ prefix prevents regex evaluation for that prefix. Otherwise, regular expressions are tested in the order listed in the configuration, and the first match wins. Preserve these modifiers and ordering intentionally when repairing a duplicate.
Choose the correct repair
Exact duplicate: remove one copy
If both blocks have the same modifier and URI and the directives are identical, retain one canonical block and delete the redundant declaration. Alternatively, remove the redundant include that loads the second copy. Removing the include is preferable when the entire fragment is duplicated; deleting a block is preferable when the fragment contains other required settings.
Before deleting anything, compare the complete blocks. Comments may differ while a directive, variable, access rule, or proxy target differs in a way that matters.
Same URI, different behavior: merge deliberately
When two blocks handle the same URI but contain different directives, Nginx will not combine them into a single logical policy for you. Create one block and place the required directives in it, resolving conflicts explicitly.
location /api/ {
proxy_pass http://api_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 20m;
}
This example is illustrative: use the upstream, headers, limits, authentication rules, and timeouts required by your application. If both original blocks set the same directive, choose one value based on the application’s intended behavior rather than relying on whichever line happens to appear last. Directives have their own inheritance and context rules; moving a line into a combined block can alter what child locations inherit.
Different applications: separate the URI space
If two services were accidentally assigned the same prefix, give them distinct prefixes or place them in the correct virtual-host server contexts. For example, an administrative application might use /admin/ while a public API uses /api/. Update upstream routing and application base paths together; changing only Nginx can produce links, redirects, or asset URLs that still point to the old path.
Regex overlap: preserve order and modifiers
Regular-expression locations can overlap without being textual duplicates. Because Nginx tests them in configuration order, moving one block above another may change which handler receives a request. Keep the intended order, retain ~ versus ~*, and verify whether a preceding ^~ prefix is meant to suppress regex evaluation.
Rank #4
Do not “solve” an overlap by changing a regex casually. A small pattern change can route static files, scripts, or security-sensitive paths to a different handler.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.A safe step-by-step merge procedure
- Back up the involved files. Copy the main file and each fragment you will edit to a protected backup location, preserving ownership and permissions where your operating system requires them.
- Write down the intended routing. List the exact URI, modifier, upstream or document root, authentication rules, headers, limits, and special-case files each original block was meant to serve.
- Identify the canonical file. Prefer the file that is intentionally enabled for this site. Remove an accidental duplicate include rather than maintaining two sources of truth.
- Build one block where appropriate. Combine non-conflicting directives under the single URI and modifier. Resolve contradictory values explicitly and check whether directives belong in the same context.
- Preserve location precedence. Keep exact matches, longest prefixes,
^~, and regex ordering consistent with the desired request flow. - Test immediately. Run
nginx -tafter each logical edit. If it fails, fix the first reported error before making another change. - Review the complete diff. Confirm that no unrelated server, include, upstream, certificate, or access-control settings changed.
- Reload through the normal service manager. The exact command depends on the operating system and service manager. Use the reload operation your host normally uses, and check its status output.
- Exercise representative URLs. Test an exact path, the affected prefix, a path that could match a regex, and an unrelated path in the same server. Check response status, upstream selection, redirects, headers, and error logs.
Validation cases that catch routing regressions
Exact versus prefix
If you have both location = /health and location /, request /health and a nearby path such as /health/extra. The first must use the exact handler; the second should follow the prefix rules you intended. A merge that removes the equals modifier can change both results.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →^~ versus regex
For a prefix protected by ^~, test a URI inside that prefix that also matches one of your regular expressions. It should remain with the prefix handler. If it reaches the regex handler, the modifier was removed, moved, or placed in a different server context.
Regex ordering
Create requests that match both a broad and a specific expression. Confirm that the specific expression appears first when that is the intended behavior. Nginx uses the first matching regex in configuration order.
Included-server boundaries
Verify that each hostname still reaches the intended server block. A location that is valid in one virtual host can be wrong if a brace or include change places it in another server context.
Common failure modes and fixes
“I deleted the line named in the error, but the error remains”
The message identifies the declaration currently being parsed, not necessarily the original copy. Search the entire include tree again, including nested files and symlink targets. You may have removed one of several duplicates or edited a file that is not the active copy.
“The same URI appears in different files, so they must be duplicates”
First compare the modifier and server context. An exact location, a prefix location, and a regex location can have different semantics even when their visible text resembles the same path. The error is about colliding declarations in one effective context; identical-looking lines in separate virtual hosts may be valid.
“The test passes, but the application behaves differently”
A successful parse proves syntax and duplicate declarations are resolved; it does not prove routing intent. Check precedence, regex order, inherited directives, upstream names, and the URLs listed in the validation cases. Inspect access and error logs while making representative requests.
“Reload fails after the fix”
Run nginx -t again and read the first error. The duplicate may have concealed a second problem such as an unmatched brace, an invalid directive context, or a missing include file. Restore the backup if necessary, then reapply one change at a time.
Quick Recap
Prevent duplicate locations in future changes
- Keep one canonical file for each virtual host and application route.
- Use descriptive fragment names and document which parent file includes each fragment.
- Do not mix explicit includes and wildcard includes for the same directory unless you have verified that no file can be selected twice.
- Check symlinks when enabling or disabling sites.
- Review the complete include tree before copying a configuration example.
- Run
nginx -tin deployment checks before any reload. - After a successful reload, test exact, prefix,
^~, and regex routes that are important to the application.
Quick recovery checklist
- Run
nginx -tand save the complete error. - Use
nginx -Vto confirm the compiled--conf-path. - Trace every
include, wildcard, nested fragment, and symlink. - Search for the repeated URI and modifier across the active tree.
- Decide whether to remove a duplicate, merge directives, separate URI prefixes, or move a block to another
server. - Preserve exact, prefix,
^~, and regex precedence. - Run
nginx -tafter each edit. - Reload with the host’s normal service manager and test representative URLs.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Free tools Windows power users keep installed
One-click scans. No signup required.

