Nginx Error: 'root directive is duplicate' — Cause, Fix, and Troubleshooting Guide
Fix nginx [emerg] 'root' directive is duplicate: two root directives in the same context. Keep one root per block and use location overrides correctly.
- #nginx
- #web-server
- #troubleshooting
- #config
Stuck on this NGINX error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
What this error means
"root" directive is duplicate is a configuration-parse error. The root directive sets the document root for a context, and NGINX allows only one root per block — one per server, and at most one per location. When a single server {} or location {} contains two root lines, NGINX cannot decide which filesystem path wins and refuses to load the config.
nginx: [emerg] "root" directive is duplicate in /etc/nginx/conf.d/example.conf:14
nginx: configuration file /etc/nginx/nginx.conf test failed
The cited line is the second root in the block. This is a pure structural error — no runtime state involved. A reload keeps the previous config live; a cold systemctl start nginx fails and the site does not come up.
How the server responds
nginx -tfails with[emerg] "root" directive is duplicate.- The error cites the file and line of the second
rootin aserverorlocation. systemctl reload nginxfails; a coldstartleaves NGINX dead.- Started after copy-pasting a block, merging two configs, or adding a
rootinside alocationthat also inherits one. - Files under the served path 404 or serve from the wrong directory once you patch it incorrectly.
- Often paired in the same edit session with
alias/rootconfusion insidelocation.
Testing the server configuration
Validate first; the emerg names the file and the line of the duplicate.
sudo nginx -t
Look at the cited block with line numbers to see both root lines:
sed -n '1,30p' /etc/nginx/conf.d/example.conf | cat -n
List every root directive across the config so you can spot two in one context:
grep -RnE '^\s*root\s' /etc/nginx/
Because include order assembles the final block, dump the resolved config and inspect the offending server/location:
sudo nginx -T | grep -nE 'server_name|location|root'
Check whether an included snippet also sets root:
grep -RnE '^\s*root\s' /etc/nginx/snippets/ /etc/nginx/conf.d/
Server configuration causes
- Copy-paste inside one block — a
server {}ends up with tworoot /var/www/...;lines after a merge or duplicated snippet. rootin both a template and an include — a base snippet setsroot, and the vhost that includes it also setsrootin the same context.- Two
rootlines in onelocation— an edit added a newrootto alocationthat already had one, instead of replacing it. - Confusing inheritance with re-declaration — a
locationlegitimately overrides the serverrootwith its own singleroot; adding a secondrootin that samelocationis the error (server-level and location-levelrootare different contexts and do not conflict). - Generated config drift — templating emitted
roottwice for the same block with the same or different paths. rootandaliasmixed up — attempting to set both arootand an extrarootwhere analiaswas intended.
The fix
Keep exactly one root per context. If a server block has two, delete the redundant one:
# /etc/nginx/conf.d/example.conf
server {
listen 80;
server_name example.com;
root /var/www/example; # ONE root for the server
index index.html;
# remove the second `root /var/www/old;` that was on line 14
location / {
try_files $uri $uri/ =404;
}
}
A location may legitimately override the server root with its own single root — that is inheritance across contexts, not a duplicate:
server {
root /var/www/example; # server-level default
location /downloads/ {
root /srv/files; # one root here — overrides for this location only
}
}
If you meant to map a URI prefix to a directory that does not repeat the prefix, use alias instead of a second root:
location /static/ {
alias /var/www/assets/; # not a second root
}
Hoist a shared root to the server level so included snippets don’t also set one — declare it once and let inner location blocks override only where needed. Validate and reload:
sudo nginx -t && sudo systemctl reload nginx
Safe configuration practice
- One
rootper context: a server-levelrootand a location-levelrootcoexist fine — two in the same block is the error. locationrootoverrides the serverrootby inheritance; you don’t need to (and can’t) declare it twice in one location.- Know when to use
aliasvsroot:aliasreplaces the matched location prefix,rootappends the full URI — mixing them causes both duplicate errors and wrong paths. - Audit
included snippets for aroot; a snippet plus a vhost both settingrootin one context collides. - Grep
^\s*root\sacross the tree after merges or bulk edits to catch a stray second declaration. - Prefer one server-level
rootand minimal per-location overrides so the document root stays easy to reason about.
Related server errors
- Nginx Error Duplicate Location — the sibling “declared twice” parse error, for
locationblocks rather thanroot. - Nginx Error Directive Not Allowed Here — a context-placement
[emerg]that appears when a directive lands in the wrong block. - Nginx Error Conflicting Server Name — another server-block declaration clash worth checking during config merges.
See the NGINX category for more guides.
Fixed it? Get 500 NGINX & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.