The Nginx “duplicate location” error means the effective configuration contains colliding location declarations in the same server context. The declarations may be in different files: Nginx combines the main configuration with every file loaded through include, so two harmless-looking fragments can become duplicates when assembled.
Need a server to try this on? Antihost offers Linux VPS plans with NVMe storage, full root access and a 14-day free trial.
Read the full nginx -t message, identify the reported file and line, trace the active include tree, then remove or deliberately merge the conflicting blocks. Test after every edit, and preserve Nginx’s exact, prefix, ^~, and regular-expression matching rules when deciding whether a merge is safe.
As an Amazon Associate I earn from qualifying purchases.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsWhat the “duplicate location” error means
A location block tells Nginx how to process a set of request URIs. Nginx expects location declarations to be organized inside a particular server context. If it encounters two declarations that collide in that context, configuration parsing can stop with an error that includes wording such as “duplicate location” and a file path with a line number.
The apparent duplicate is often not visible in one file. A main nginx.conf may load conf.d/*.conf, sites-enabled/*, and nested include files. If the same fragment is enabled twice, or two fragments define the same URI, the combined configuration contains the collision even though each fragment looks valid by itself.
#1 Best Overall
Typical causes
- The same
locationblock was copied twice into one virtual-host file. - Two enabled site fragments define the same URI, such as
location /api/. - A wildcard include and an explicit include both load one file.
- Two symlinked files point to the same configuration fragment.
- A generated file was left enabled after a manually maintained replacement was added.
- Two blocks use the same URI and modifier but were intended for different applications.
The parser reports the location where it detected the conflict, not necessarily the first declaration. You must inspect the earlier declaration as well as the line named in the error.
Start with the complete parser error
Run the configuration test using the Nginx installation’s normal command:
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →nginx -t
Do not act on only the final line. Capture the complete output, including:
- The path of the file being parsed.
- The line number reported by Nginx.
- The exact URI and modifier shown in the message, when present.
- Any preceding warning that identifies an unexpected include or context.
If Nginx reports more than one problem, fix the first reported error and run nginx -t again. Later diagnostics can be misleading because the parser may not have reached the rest of the configuration.
Confirm which configuration tree is active
Before searching files, verify that you are looking at the configuration Nginx actually uses. The binary can be compiled with a nonstandard default path. Run:
nginx -V
Inspect the output for the compiled --conf-path= value. That path identifies the default main configuration file for that installation. If your service manager starts Nginx with an explicit -c option, use that service definition’s path instead of assuming the compiled default is active.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows 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 reinstallPermissions can also affect your investigation. Read the files as the same account or with the same privilege level used to administer the service. A shell search that cannot read a directory may silently omit the file containing the second declaration.
Rank #2
Trace every include that contributes to the effective configuration
Open the active main configuration and follow each include directive. Treat the include tree as one large configuration file. Pay particular attention to common patterns such as:
conf.d/*.confsites-enabled/*- Explicit includes of a file that is also matched by a wildcard.
- Includes nested inside a site file or another feature-specific fragment.
Nginx supports splitting configuration into feature-specific files, but that convenience makes duplicate loading possible. Write down each include path as you follow it. If a wildcard is used, expand it mentally or with a directory listing and check whether a symlink resolves to a file already included elsewhere.
Check for the same file loaded twice
Look for an explicit include such as include /etc/nginx/sites-enabled/example.conf; alongside a wildcard that already matches example.conf. Also check whether both a real file and a symlink to that file are matched. Keep one canonical copy and remove the redundant include or symlink. Do not delete a file until you have confirmed which path the service expects.
Check server context, not just filenames
Two locations with identical text are only a duplicate when they are parsed in the same relevant server context. Compare the surrounding braces, including the server_name, listen settings, and any nested includes. A block intended for one virtual host may have been placed inside another because of a missing or misplaced brace.
Search the expanded configuration tree for repeated locations
Once you know the active root and include directories, search all participating files for the URI and modifier named by the error. For example:
grep -RInE 'location[[:space:]]+(=|^~)?[[:space:]]*/api/' /etc/nginx
Adapt the path to the configuration root reported by nginx -V. To inspect common forms, search for exact, prefix, and regular-expression declarations separately:
grep -RIn 'location = /health' /etc/nginx
grep -RIn 'location /api/' /etc/nginx
grep -RIn 'location ~ .php$' /etc/nginx
Search results show where text occurs; they do not by themselves prove that every match is active. Exclude archived files, editor backups, and documentation that are not reached by an include. Then verify each candidate against the include tree and the enclosing server block.
Recommended Free Tools
Rank #3
Recognize the modifier as part of the identity
Do not compare only the URI text. These declarations have different modifiers and matching behavior:
| Declaration | Meaning to preserve while diagnosing |
|---|---|
location = /health |
Exact match for one URI. |
location /api/ |
Prefix location for URIs beginning with that path. |
location ^~ /assets/ |
Prefix location that suppresses regular-expression evaluation when selected. |
location ~ .php$ |
Case-sensitive regular-expression location. |
location ~* .jpg$ |
Case-insensitive regular-expression location. |
Two declarations can collide even when their visible text differs slightly, and two declarations that look similar can have different routing effects because of a modifier. Record the full declaration before changing it.
Understand Nginx’s location-selection rules before merging
Merging blocks changes more than formatting. Nginx chooses a location using a defined sequence:
- An exact
=match is selected immediately and stops the search. - Nginx finds the longest matching prefix.
- If that prefix uses
^~, regular-expression locations are not tested for the request. - Otherwise, regular-expression locations are tested in the order listed in the configuration, and the first matching expression wins.
These rules mean that a duplicate repair must preserve both the modifier and the order of regular-expression blocks. Moving a regex block above another can change which application receives a request. Removing ^~ can allow a regex to take traffic that previously stayed under a prefix. Converting an exact match into a prefix also broadens the set of requests affected.
Use a decision table for the conflicting pair
| What you find | Usually safest repair | Main risk |
|---|---|---|
| Byte-for-byte duplicate in the same server | Delete the redundant copy and retain one canonical block. | The copies may contain comments or later edits that are not actually identical. |
| Same URI and modifier, different directives | Combine the intended directives into one block after reviewing inheritance and conflicts. | Duplicate directives may override one another or create an invalid combination. |
| Different applications need the same prefix | Give each application a distinct URI prefix, or place one block in the correct server context. | Changing a public URI can require application and client changes. |
| Overlapping regex locations | Keep only the intended expressions and place them in deliberate order. | The first matching expression wins, so order changes routing. |
Repair an exact duplicate
If both declarations have the same modifier and URI and one is a genuine copy, keep one block. Remove the duplicate from the file that should no longer define that behavior, or remove the redundant include that loads it twice.
Use version control or a dated backup before editing. Preserve the surviving block’s comments and any directives that are not present in the copy. A quick visual comparison is not enough: compare nested directives such as proxy_pass, root, try_files, access controls, cache settings, and custom headers.
After deleting one copy, run:
nginx -t
If the parser now reports a different duplicate, repeat the include-tree investigation. Configuration errors are reported progressively; one successful deletion does not prove that the entire tree is clean.
Rank #4
Merge blocks with the same URI deliberately
When two blocks describe the same URI but perform different work, Nginx will not combine them for you. Create one block containing the directives that are actually required, then remove the second declaration.
Inventory directives before combining
- List directives that are identical in both blocks and retain one copy.
- Identify directives with different values, such as two upstream targets or two document roots. Choose the value that matches the intended application; do not keep both.
- Check directives whose behavior depends on inheritance from
http,server, or parentlocationcontexts. - Look for mutually exclusive processing paths, such as proxying in one block and serving local files in the other.
- Check nested locations. A merge can change which parent directives they inherit.
Example: consolidating one prefix
location /api/ {
proxy_set_header Host $host;
proxy_pass http://api_backend;
}
location /api/ {
proxy_set_header X-Request-ID $request_id;
proxy_read_timeout 60s;
}
The safe result is a single block only if all four directives are intended for the same API:
location /api/ {
proxy_set_header Host $host;
proxy_set_header X-Request-ID $request_id;
proxy_pass http://api_backend;
proxy_read_timeout 60s;
}
This example is illustrative of the merge method, not a recommendation to use particular proxy settings. In a real configuration, confirm that the upstream, headers, timeout, and authentication behavior belong together.
Do not merge incompatible application behavior
If one block serves static files and the other proxies to an application, combining both without a routing plan can send every request down the wrong path. Use a more specific child location for a deliberate exception, assign a distinct URI prefix, or move one block into the server context that owns that application. Keep the public URI stable unless you can update all clients and application routes.
Handle regex overlap without changing request routing accidentally
Regular-expression locations are evaluated in configuration order. The first matching expression wins. Therefore, two regex declarations that both match a request are not interchangeable, even if Nginx accepts them after you remove another conflict.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Before editing regex locations:
- Write down representative URIs, including ones that match both expressions.
- Determine which expression is supposed to win for each URI.
- Place the expressions in that order in the same server configuration.
- Preserve case sensitivity:
~and~*are not equivalent. - Retest a URI that should match each expression and one that should match both.
If a prefix uses ^~, verify that keeping it is intentional. It prevents regex evaluation for requests selected by that prefix, so removing the modifier can redirect requests into a regex block.
Move blocks that belong to different applications or servers
A duplicate can expose a structural mistake rather than a simple copy. For example, two applications may both have been assigned / or /api/ inside one virtual host. Decide which URI space each application owns. Then either:
Best Value
- Assign distinct prefixes, such as separate application paths.
- Use separate
servercontexts with the appropriate hostnames. - Place a more-specific child location inside the parent application’s routing design.
Do not solve the parser error by arbitrarily deleting one application’s block. That makes the configuration test pass while silently taking an application offline. Confirm the intended hostname, URI, upstream, and static-file behavior before relocating anything.
Validate the complete configuration after each edit
Run nginx -t after every logical change. A successful test means the configuration is syntactically acceptable to the installed Nginx binary; it does not prove that requests route to the intended application.
Free tools Windows power users keep installed
One-click scans. No signup required.
Once the test passes, reload Nginx through the host’s normal service manager. The exact reload command depends on the operating system and service manager, so use the method already used to operate that installation rather than assuming a universal command.
Exercise representative requests
After the reload, test at least one request for each affected matching class:
- The exact URI, such as
/health, without an extra path segment. - A URI under the affected prefix, such as
/api/users. - A request that should be handled by a
^~prefix instead of a regex. - A URI that matches each regular-expression location.
- A URI that could match two regexes, confirming that the intended first expression wins.
Check the response status, headers, upstream or static content, and error logs. A 200 response alone is not sufficient if it comes from the wrong application or bypasses an access rule.
Common diagnostic dead ends and their fixes
Searching only the file named in the error
The named file is where the parser noticed the conflict. The original declaration may be in another included file. Search the entire active include tree and inspect both locations.
Searching the whole filesystem without tracing includes
Broad searches produce false positives from backups, package examples, and disabled sites. Start with the compiled configuration path and follow only files that the active configuration can include.
Removing a block because it is “similar”
Similar URIs can represent different modifiers or applications. Compare the complete declaration, enclosing server, directives, and intended request paths before deleting anything.
Changing regex order while fixing a duplicate
Regex order is routing logic. Preserve the intended sequence and test overlapping requests after the edit.
Stopping after a syntax pass
The parser cannot detect every semantic routing mistake. Reload only after the test passes, then exercise exact, prefix, and regex cases and inspect the resulting behavior.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Prevent future duplicate-location failures
- Maintain one canonical file for each virtual host or feature.
- Document which wildcard directories are active and avoid explicit includes of files already covered by those patterns.
- Check symlinks before enabling a site fragment.
- Keep generated configuration separate from hand-maintained files and remove obsolete generated output.
- Use version control so additions and deletions can be reviewed as a pair.
- Run
nginx -tin deployment checks before reloading the service. - When adding a location, record its modifier, URI, owning server, and intended precedence.
- For regex locations, review ordering whenever a new expression is introduced.
A concise recovery checklist
- Run
nginx -tand save the complete error, file path, and line number. - Run
nginx -Vand confirm the compiled--conf-path=. - Open the active main file and follow every nested
include. - Search active files for the same URI and modifier, including symlinked fragments.
- Confirm that the declarations are in the same
servercontext. - Delete the redundant copy, merge compatible directives, or separate applications by server or URI.
- Preserve exact, prefix,
^~, and regex precedence. - Run
nginx -tagain and fix the first remaining error. - Reload through the normal service manager only after the test succeeds.
- Exercise representative exact, prefix, and regex URLs and verify the response path.
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.




