October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk11 min

How to Fix the Nginx “Duplicate Location” Configuration Error

A duplicate-location error usually comes from two colliding declarations in the same effective server configuration. Learn how to trace includes, identify the real duplicate, repair it without changing routing, and validate the reload.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

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

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

Typical causes

  • The same location block 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:

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

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

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

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/*.conf
  • sites-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.

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

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.

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

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:

  1. An exact = match is selected immediately and stops the search.
  2. Nginx finds the longest matching prefix.
  3. If that prefix uses ^~, regular-expression locations are not tested for the request.
  4. 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.

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

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.

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.

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

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 parent location contexts.
  • 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.

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

Before editing regex locations:

  1. Write down representative URIs, including ones that match both expressions.
  2. Determine which expression is supposed to win for each URI.
  3. Place the expressions in that order in the same server configuration.
  4. Preserve case sensitivity: ~ and ~* are not equivalent.
  5. 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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:

  • Assign distinct prefixes, such as separate application paths.
  • Use separate server contexts 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.

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

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.

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

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.

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

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 -t in 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

  1. Run nginx -t and save the complete error, file path, and line number.
  2. Run nginx -V and confirm the compiled --conf-path=.
  3. Open the active main file and follow every nested include.
  4. Search active files for the same URI and modifier, including symlinked fragments.
  5. Confirm that the declarations are in the same server context.
  6. Delete the redundant copy, merge compatible directives, or separate applications by server or URI.
  7. Preserve exact, prefix, ^~, and regex precedence.
  8. Run nginx -t again and fix the first remaining error.
  9. Reload through the normal service manager only after the test succeeds.
  10. 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.

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 from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

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.