Fix nginx [emerg] duplicate location "/path" in ...
nginx -t "[emerg] duplicate location" means one server block defines the same location path twice. Find both files with grep -Rn and keep one.
2026/10/30 22:41:46 [emerg] 11#11: duplicate location "/api/" in /etc/nginx/locations/api.conf:1nginx: [emerg] duplicate location "/api/" in /etc/nginx/locations/api.conf:1nginx: configuration file /etc/nginx/nginx.conf test failedThe same server block defines a location with the same path twice. The file and line in the message (api.conf:1) are the later definition; the earlier one is somewhere else. It usually happens when include pulls in the same fragment twice, or a leftover copy sits in an included directory. Find both and keep one.
[1] Find the path from the message with -R. With a config spread over includes you would like to dump everything with nginx -T, but while the config test is failing it prints nothing:
$ nginx -T 2>/dev/null | wc -l0So search the files directly. Use capital -R: lowercase -r skips symlinks such as those in sites-enabled/.
Adding location to the search is tempting, but it misses things:
$ grep -Rn "location /api/" /etc/nginx//etc/nginx/locations/api.conf:1:location /api/ {Only one hit. The "/api/" in the error message has no modifier such as = or ^~, so a counterpart written as location ^~ /api/ or location = /api/ is not matched. Search for the path alone and read the modifiers in the results:
$ grep -Rn "/api/" /etc/nginx/ --include="*.conf"/etc/nginx/locations/api.conf:1:location /api/ {/etc/nginx/locations/api-old.conf:1:location ^~ /api/ {The same path is in both api.conf and api-old.conf. One has ^~, but both are prefix locations, so they collide. The old in the name suggests a leftover copy. If other lines such as proxy_pass show up, look only at the location lines.
[2] Check how both files reach the same server. In this example one include line inside the server block did it:
server { listen 80; server_name example.com; include /etc/nginx/locations/*.conf;}$ ls /etc/nginx/locations/api-old.confapi.conf*.conf pulls in every matching file in the folder, so the copy (api-old.conf) becomes live config too. Files you thought you deleted, editor copies, and old versions left by a deploy tool overlap this way. A name that does not end in .conf (api.conf.bak) is not matched by this include.
[3] Decide which one to keep. If the two definitions differ, first confirm which version is the intended one. nginx does not pick one of them here; the config test simply fails. nginx -t only checks, so if you have not reloaded, the running process still has the previous config.
[4] Move the unwanted one out of the included folder. Moving instead of deleting keeps a way back.
$ mkdir -p /var/backups/nginx && mv /etc/nginx/locations/api-old.conf /var/backups/nginx/$ nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulWhen it passes, apply it with sudo systemctl reload nginx. To undo, move the file back into locations/, test and reload.
[5] Some pairs are not conflicts. This table is the result of putting two lines in one server block and running nginx -t:
| Two locations | Result |
|---|---|
location /a · location /a | error |
location = /a · location = /a | error |
location /a · location ^~ /a | error (^~ is also a prefix location) |
two nested location /a/b (inside the same /a/) | error |
location = /a · location /a | passes (exact and prefix are separate) |
location /a · location /a/ | passes (different paths) |
location /a · location /A | passes (case-sensitive, Linux) |
two location ~ \.php$ | passes (regex locations are not checked for duplicates; the first one written wins) |
location /a in two different server blocks | passes |
Note that regex locations pass. Writing the same regex twice raises no error, and the later one is silently never used. The official nginx documentation says regular expressions are checked in the order they appear in the configuration and the search stops at the first match. No error does not mean no overlap.
For overlapping server_name see conflicting server name … ignored; for overlapping default_server see a duplicate default server.
Analyze your Nginx log in the browser ↗ — nothing is uploaded.