Skip to content
🎉 Launch sale:50% off everything over $22 — automatically applied at checkout· ends Aug 2Shop the sale →
DevOps AI ToolKit
Newsletter
All guides
AI for NGINX By James Joyner IV · · 8 min read Last reviewed Jul 2026

Nginx Error: 'root directive is duplicate' — Cause, Fix, and Troubleshooting Guide

Quick answer

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
Free toolkit

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 -t fails with [emerg] "root" directive is duplicate.
  • The error cites the file and line of the second root in a server or location.
  • systemctl reload nginx fails; a cold start leaves NGINX dead.
  • Started after copy-pasting a block, merging two configs, or adding a root inside a location that 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/root confusion inside location.

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 two root /var/www/...; lines after a merge or duplicated snippet.
  • root in both a template and an include — a base snippet sets root, and the vhost that includes it also sets root in the same context.
  • Two root lines in one location — an edit added a new root to a location that already had one, instead of replacing it.
  • Confusing inheritance with re-declaration — a location legitimately overrides the server root with its own single root; adding a second root in that same location is the error (server-level and location-level root are different contexts and do not conflict).
  • Generated config drift — templating emitted root twice for the same block with the same or different paths.
  • root and alias mixed up — attempting to set both a root and an extra root where an alias was 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 root per context: a server-level root and a location-level root coexist fine — two in the same block is the error.
  • location root overrides the server root by inheritance; you don’t need to (and can’t) declare it twice in one location.
  • Know when to use alias vs root: alias replaces the matched location prefix, root appends the full URI — mixing them causes both duplicate errors and wrong paths.
  • Audit included snippets for a root; a snippet plus a vhost both setting root in one context collides.
  • Grep ^\s*root\s across the tree after merges or bulk edits to catch a stray second declaration.
  • Prefer one server-level root and minimal per-location overrides so the document root stays easy to reason about.

See the NGINX category for more guides.

Free download · 368-page PDF

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?

Free download · 368-page PDF

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.