Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

Typical causes include:

  • Copying a complete location block 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 server even 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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 server context 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.Support on Ko-Fi

A safe step-by-step merge procedure

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. Preserve location precedence. Keep exact matches, longest prefixes, ^~, and regex ordering consistent with the desired request flow.
  6. Test immediately. Run nginx -t after each logical edit. If it fails, fix the first reported error before making another change.
  7. Review the complete diff. Confirm that no unrelated server, include, upstream, certificate, or access-control settings changed.
  8. 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.
  9. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

^~ 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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

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 -t in deployment checks before any reload.
  • After a successful reload, test exact, prefix, ^~, and regex routes that are important to the application.

Quick recovery checklist

  1. Run nginx -t and save the complete error.
  2. Use nginx -V to confirm the compiled --conf-path.
  3. Trace every include, wildcard, nested fragment, and symlink.
  4. Search for the repeated URI and modifier across the active tree.
  5. Decide whether to remove a duplicate, merge directives, separate URI prefixes, or move a block to another server.
  6. Preserve exact, prefix, ^~, and regex precedence.
  7. Run nginx -t after each edit.
  8. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.