October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
VGSources
Blog

How to Fix NGINX’s “Duplicate Location” Configuration Error

A practical, step-by-step guide to finding duplicate NGINX location blocks across included files, choosing between deletion and merging, and validating routing after reload.
Length9 min Posted Quest giverVGSources Team
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The fix is to find the two location declarations that end up in the same effective server configuration, then remove one or deliberately merge their directives. They may be in separate files: NGINX expands every active include, so a copied block, an enabled site fragment, a wildcard, or a symlink can load the same URI twice.

Do not guess which file is active or merge blocks by URI alone. Read the complete nginx -t message, verify the compiled configuration path, trace the include tree, preserve location-selection rules, and test again after each edit.

As an Amazon Associate I earn from qualifying purchases.

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

What the “duplicate location” error means

NGINX uses one or more location contexts inside a server context to define how requests for particular URI sets are processed. The parser reports a duplicate-location error when declarations that collide are present in the same effective server configuration. “Effective” matters: two blocks can be in different files but become one configuration after NGINX follows all active include directives.

The usual causes are straightforward:

  • The same block was copied into a file twice.
  • A site fragment was enabled while an older copy remained enabled.
  • A wildcard such as conf.d/*.conf matches a file that is also included explicitly.
  • A symlink or nested include causes one fragment to be read through two paths.
  • Two applications were assigned the same URI and modifier inside one server block.

The path and line number in the error identify where the parser encountered the later declaration. The earlier declaration may be elsewhere in the include tree, so deleting the line named in the error without checking its counterpart can remove the wrong configuration.

Start with the complete test error

Run the installation’s configuration test and capture every line:

nginx -t

Read the entire output, not only the final “test failed” line. A duplicate-location message normally includes the file path and line number where NGINX detected the collision. Record:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • the URI in the message, such as /api/ or /health;
  • the modifier, if shown, such as =, ^~, or ~;
  • the reported file path and line number;
  • any preceding warning or error that might indicate a different include problem.

Fix the first reported error before investigating later messages. NGINX stops parsing at the first fatal configuration problem, so subsequent output can be incomplete until that one is resolved.

Confirm which configuration tree NGINX is using

Before opening files, verify the binary’s compiled defaults:

nginx -V

In the version and build options, look for --conf-path=. NGINX documents this option as the way to identify the compiled default configuration path. A common mistake is editing a project copy of nginx.conf while the running binary reads a distribution-specific path, a container-mounted file, or a custom path selected by the service manager.

If your service starts NGINX with an explicit -c option, that path takes precedence for that invocation. Check the service definition, container entrypoint, or startup script when the path shown by nginx -V does not match the file you expected. The goal is to establish one canonical root configuration before changing anything.

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

Trace every include that can load the block

Open the active main configuration and follow each include directive recursively. Pay special attention to patterns commonly used for site and feature fragments:

include conf.d/*.conf;
include sites-enabled/*;

The exact directories differ by installation. What matters is the complete tree, including nested includes. For each include, make a checklist of the files it can match and mark whether each file is a regular file or a symlink.

Look for double inclusion

A fragment can be loaded twice even when it contains only one location block. Typical paths are:

  • the file is matched by a wildcard and also named explicitly;
  • both a real file and a symlink to it are matched;
  • a directory is included from the main file and again from a nested site file;
  • two enabled-site symlinks point to the same source file.

Keep one canonical include path. Remove the redundant include or duplicate symlink rather than maintaining two copies that can drift apart.

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

Search for the URI and modifier together

Search the entire active tree for the exact declaration, not only for the word location. For example, search separately for:

location /api/
location = /health
location ~ .php$

Include comments and generated fragments in your review. A commented line cannot cause the error, but a templating step may have produced an uncommented copy in a file you did not expect. Compare the enclosing server blocks as well; identical locations in different servers are normally valid, while collisions inside one server are the important case here.

Understand location matching before merging anything

A location’s URI and modifier determine request routing. NGINX’s documented selection order is:

  1. An exact match using = is selected immediately and stops the search.
  2. For prefix locations, NGINX keeps the longest matching prefix.
  3. A longest prefix marked ^~ suppresses regular-expression evaluation for that prefix.
  4. Otherwise, regular-expression locations are tested in the order listed in the configuration file; the first matching expression wins.

These rules mean that two blocks with similar-looking paths are not automatically interchangeable. A merge can change which upstream, root, rewrite, access rule, or header handles a request even when the resulting file still passes syntax validation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Declaration form Selection behavior Merge concern
location = /health Exact URI; search stops on a match. Do not turn it into a prefix block unless every longer URI should receive the same handling.
location /api/ Prefix; the longest matching prefix is chosen before regex processing. Check whether another, more-specific prefix exists.
location ^~ /assets/ Prefix; matching regular expressions are skipped. Removing ^~ can route assets into a regex block.
location ~ .php$ Regular expression; first matching regex in file order wins. Moving or combining regex blocks changes precedence.

Choose the correct repair pattern

Exact duplicate: remove one block

If both declarations have the same modifier and URI and one is a copy, retain the intended block and delete the redundant declaration. Compare all directives before deleting: a copied-looking block may contain an additional security header, logging rule, timeout, or access control line.

When the blocks are truly identical, keeping one canonical definition is safest. Add a short comment explaining where the block belongs if future edits are likely to recreate the duplicate.

Same URI, different behavior: merge deliberately

Sometimes two teams added the same URI for different reasons. NGINX will not infer how to combine their intent. Create one location block and move the required directives into it, resolving conflicts explicitly.

Use this checklist while merging:

  • Identify which content handler should win: proxy_pass, fastcgi_pass, static files, or another handler.
  • Compare root, alias, index, rewrite, and try-files behavior.
  • Resolve conflicting proxy or FastCGI parameters instead of leaving both versions.
  • Review access controls, authentication, rate limits, request methods, and body-size limits.
  • Preserve required headers, logging choices, caching directives, and timeout values.
  • Check inheritance from the surrounding server and http contexts.

Do not assume that placing two conflicting directives in one block reproduces either original behavior. Some directives override earlier values, some can appear multiple times, and some are only valid in particular contexts. Follow the directive’s documented context and choose one intentional value where the configurations disagree.

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.

Different applications: give them distinct routing space

If two applications genuinely need separate handlers, assign distinct URI prefixes or place each location in the correct server context. For example, one application might own /api/ while another owns /admin/. Check trailing slashes and longer prefixes so that a request cannot accidentally move from one application to the other.

If the applications use different hostnames, separate server blocks may be the correct design. A location declaration in one server does not collide with an otherwise identical declaration in another server merely because the text is the same.

Regex overlap: preserve order and modifiers

Regular expressions are order-sensitive. NGINX checks them in configuration order and uses the first matching expression. If two regex blocks overlap, combine them only after deciding which expression should win, or keep separate blocks in a documented order.

Do not “fix” a duplicate by changing a modifier casually. Converting location ~ to a prefix, adding or removing ^~, or changing an exact match to a prefix changes the request-selection algorithm. Such a change is a routing design decision, not a parser workaround.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

A safe, repeatable repair procedure

  1. Back up the active configuration. Copy the main file and affected fragments, preserving symlinks and permissions. Keep the backup outside any wildcard include directory so it cannot be loaded accidentally.
  2. Run nginx -t and save the full error. Note the URI, modifier, path, and line reported.
  3. Verify the configuration root. Use nginx -V to check --conf-path=, then confirm any explicit configuration path used by the service.
  4. Trace includes recursively. Record every file that can be loaded, including nested wildcard matches and symlink targets.
  5. Find both declarations. Search for the exact URI and modifier, then inspect the surrounding server context in each result.
  6. Classify the collision. Decide whether it is an accidental copy, a double include, two behaviors that must be merged, separate applications that need different prefixes, or overlapping regexes.
  7. Make one focused edit. Remove the redundant path or create one intentional block. Avoid unrelated formatting or routing changes during the repair.
  8. Test immediately. Run nginx -t again. If it reports another error, fix that first before making further changes.
  9. Repeat until the test passes. A successful test means the configuration parses; it does not prove that every request is routed correctly.
  10. Reload through the host’s normal service manager. The exact command depends on the operating system and service setup. Use the mechanism that normally controls this NGINX instance, and check its status or logs for a failed reload.
  11. Exercise representative URLs. Test the exact URI, the relevant prefix, a longer prefix, and each overlapping regex case. Confirm status codes, upstream selection, redirects, headers, and access behavior.

Worked investigation examples

An enabled site and a wildcard both load the same file

Suppose the main file includes both conf.d/*.conf and sites-enabled/*. A file containing location /api/ is copied into conf.d/api.conf, while a symlink to that same file exists in sites-enabled/. The parser reaches the second copy and reports a duplicate.

The repair is to choose one directory as the canonical source, remove the duplicate symlink or exclude the file from one wildcard, then retest. Editing the location body will not solve a double include; the same bytes would still be parsed twice.

Two blocks share a URI but serve different handlers

One block sends /media/ to an upstream and another serves files from disk. Deleting either block silently breaks one application. Instead, determine whether the applications should use different prefixes, different hostnames, or one combined handler. If the URI must remain shared, design an explicit rule that distinguishes the requests; do not rely on declaration order unless the resulting regex or prefix behavior is intentional and documented.

A regex change appears to fix the parser but breaks requests

Changing one regex modifier can eliminate the duplicate message while allowing requests to fall into another regex location. Because regex locations are evaluated in file order, compare the complete set of expressions and test URLs that match one or more of them. A parser-clean configuration is not a routing-correct configuration.

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

Common mistakes and how to recover

  • Editing the wrong nginx.conf: Recheck --conf-path= and the service’s explicit configuration option.
  • Deleting only the reported line: Inspect both declarations and their directives before choosing which one to keep.
  • Searching one directory: Follow every include, including nested files and symlink targets.
  • Keeping two copies “for safety”: A backup inside an included wildcard is not a backup; it is another active configuration candidate.
  • Assuming a passing syntax test proves behavior: Reload and request representative exact, prefix, and regex URLs.
  • Changing precedence accidentally: Preserve =, ^~, prefix, and regex semantics unless a routing change is the explicit goal.
  • Reloading before testing: Always run nginx -t after each edit so a bad change does not replace a working configuration.

Prevent the error from returning

  • Keep one canonical file for each location and document which include loads it.
  • Use a consistent convention for enabled sites, such as symlinks, rather than mixing explicit and wildcard inclusion for the same files.
  • Review generated configuration output for duplicate fragments before deploying it.
  • Keep backups and temporary files outside active include globs.
  • When adding a location, search the effective tree for the same URI and modifier first.
  • Comment intentional regex ordering and ^~ choices so later edits do not change precedence accidentally.
  • Make configuration testing part of deployment and reload only after the test succeeds.

What a successful fix looks like

The final configuration has one intentional declaration for each colliding location in a given server context, or a clearly designed set whose modifiers and URIs do not conflict. nginx -t completes successfully, the normal service-manager reload succeeds, and representative requests reach the intended handlers. The durable solution is not merely to silence the parser: it is to make the include tree unambiguous and preserve the location-selection rules that your applications depend on.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.