Sub topic 3
Giving Each Site Its Own Document Root
This chapter assumes you can use the Linux command line and sudo, know how file ownership and permissions work, and have Nginx installed from your distribution's packages and running under systemd. You should also know what the HTTP Host header is. Here you give each site a separate directory of files, so that one server block can never serve another site's content.
What a document root is
A document root is the directory on disk that Nginx treats as the top of a site. The root directive sets it. Nginx builds the file path for a request by appending the request URI to the root.
With root /var/www/example.com/html;, a request for /about/team.html maps to /var/www/example.com/html/about/team.html. Nginx never serves files outside the root unless a directive such as alias or a symlink inside the root points elsewhere.
Each server block should have its own root. If two sites share a root, they serve identical files. If a block has no root at all, it uses the compiled-in default, which depends on how Nginx was built. Packages from Debian and Ubuntu typically use /usr/share/nginx/html or /var/www/html. Relying on that default is a common cause of "the wrong content appears" problems.
A directory layout that scales
A widely used convention is one directory per domain under /var/www, with the public files in an html subdirectory:
/var/www/
├── example.com/
│ └── html/
│ └── index.html
└── example.org/
└── html/
└── index.htmlThe html subdirectory lets you keep non-public material beside the public files. For example, /var/www/example.com/backups or /var/www/example.com/private sits outside the document root, so Nginx cannot serve it by accident.
The name /var/www is a convention, not a requirement. Any path that the Nginx worker user can read will work. Whatever you choose, use the same pattern for every site so the layout is predictable.
A server block with its own root
This server block serves static files for one site from its own root:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Each directive has a job:
rootsets the base directory for the site.indexlists the file names Nginx tries when the request ends in a slash, such as/or/docs/.try_files $uri $uri/ =404checks, in order, for a file matching the URI, then a directory matching the URI, and finally returns a 404 error.
The listen and server_name lines decide when this block is used. They are covered in the next chapters. For now, note that root belongs to the block, not to the whole server.
Where to put root
You can put root at the server level, as above. Nginx also allows it inside a location. A root at the server level is inherited by every location in that block that does not set its own. Prefer the server level, because a single definition is easy to audit. Set it in a location only when one URL prefix really needs different files.
Creating the directories and test pages
Create both document roots and a distinct test page in each. The different page text lets you see at a glance which site answered.
sudo mkdir -p /var/www/example.com/html /var/www/example.org/html
echo '<h1>example.com</h1>' | sudo tee /var/www/example.com/html/index.html
echo '<h1>example.org</h1>' | sudo tee /var/www/example.org/html/index.html
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxThe first three commands build the roots. The two ln -s lines enable the site files using the sites-available and sites-enabled layout that Debian and Ubuntu packages use. This assumes you have already written /etc/nginx/sites-available/example.com and example.org. On distributions that use conf.d instead, you place the file in /etc/nginx/conf.d/ with a .conf suffix and skip the symlinks. The last line tests the configuration and reloads Nginx only if the test passes. The next chapter covers that workflow in detail.
Ownership and permissions
Nginx serves files as its worker user, not as root. The master process starts as root, but the workers drop to the user named by the user directive in /etc/nginx/nginx.conf. On Debian and Ubuntu this is www-data. On RHEL-family systems it is nginx. Check yours:
grep -E '^\s*user\s' /etc/nginx/nginx.conf
ps -o user,comm -C nginxFor a file to be served, the worker user needs two things:
- Read permission on the file.
- Execute (search) permission on every directory in the path, from
/down to the file's directory.
The second point is the one people miss. A directory without the execute bit for the worker cannot be traversed, even if the file inside is world-readable.
A safe, common arrangement is for a deploy user to own the files, with permissions that let others read them:
sudo chown -R deploy:deploy /var/www/example.com
sudo find /var/www/example.com/html -type d -exec chmod 755 {} +
sudo find /var/www/example.com/html -type f -exec chmod 644 {} +Here deploy is a hypothetical account that publishes content. Directories get mode 755 and files get mode 644. The worker user falls under "others", so it can read files and enter directories but cannot modify anything. That is what you want for static content. Nginx has no reason to write to a document root, and a read-only view limits the damage if the worker process is compromised.
Do not fix a permission problem with chmod 777. It lets any local user change your site's files.
Checking the whole path
The namei tool lists the permissions of every component of a path, which makes a blocked directory easy to spot:
namei -l /var/www/example.com/html/index.htmlYou can also test as the worker user directly:
sudo -u www-data cat /var/www/example.com/html/index.htmlIf this fails with "Permission denied", Nginx will fail too.
Home directories and SELinux
Putting a document root under /home/alice/site often fails. The home directory is typically mode 700 or 750, which blocks the worker user from traversing it. Prefer /var/www.
On systems with SELinux enforcing, such as RHEL, Rocky and Fedora, correct Unix permissions are not enough. Files must carry the right security label, httpd_sys_content_t, to be readable by the web server. Files created in a nonstandard location usually have the wrong label. Check with ls -Z, and set it with semanage fcontext followed by restorecon. The Nginx error log reports "Permission denied" in this case, the same as for ordinary permission failures, so check getenforce before debugging file modes.
index files and directory requests
When a request URI ends in /, Nginx looks for each name in the index directive in order and serves the first one that exists:
index index.html index.htm;If none exists and autoindex is off, which is the default, Nginx returns 403 Forbidden. It refuses to list a directory unless you turn on autoindex on;. A 403 on a directory with no index file is therefore normal behavior, not a fault. Either add an index file or enable autoindex for that location if you really want a listing.
How the three failure codes differ
The status code tells you where to look.
| Symptom | Likely cause | First check |
|---|---|---|
| 404 Not Found | The mapped file path does not exist | Compare root plus URI with the real file |
| 403 Forbidden | Path exists but cannot be read, or directory has no index | `namei -l`, then the error log |
| Wrong content | The request matched a different server block or a different root | Which block answered, and its root |
The error log names the exact path Nginx tried, so read it first:
sudo tail -n 20 /var/log/nginx/error.logA line such as open() "/var/www/example.com/html/missing.html" failed (2: No such file or directory) shows the full path. Errno 2 means the file is absent. (13: Permission denied) means a permission or SELinux problem.
Root versus alias
Beginners often confuse root with alias. They build paths differently.
- With
root, the full URI is appended:location /static/ { root /data; }serves/static/a.cssfrom/data/static/a.css. - With
alias, the matched location prefix is replaced:location /static/ { alias /data/assets/; }serves/static/a.cssfrom/data/assets/a.css.
When using alias for a prefix location, end both the location and the alias path with a slash. A mismatch in trailing slashes produces broken paths and has led to real path-traversal vulnerabilities. Use root unless you need the prefix removed.
Symlinks and shared content
Nginx follows symbolic links inside a document root by default. That is convenient for release directories such as /var/www/example.com/html -> /var/www/example.com/releases/2026-11-28, which you can switch atomically by replacing the link. It is also a risk if untrusted users can create links that point to sensitive files. The disable_symlinks directive restricts this, for example disable_symlinks if_not_owner;.
Verifying that each root is used
Confirm that each block really serves its own directory. The --resolve option makes curl send the right name without any DNS change:
curl --resolve example.com:80:127.0.0.1 http://example.com/
curl --resolve example.org:80:127.0.0.1 http://example.org/The first command should print <h1>example.com</h1> and the second <h1>example.org</h1>. If both print the same page, the second request fell into the first block, which means its server_name or listen is wrong. The chapter on request selection explains why.
Exercise
A request for http://example.com/docs/ returns 403 Forbidden. The error log says directory index of "/var/www/example.com/html/docs/" is forbidden. The directory exists and has mode 755. What is the most likely cause, and what are two ways to fix it?
Show answer
The docs directory contains none of the files named in the index directive, and autoindex is off, so Nginx refuses to list it. Fix it by adding an index file such as index.html to docs, or by enabling autoindex on; in a location for that path if a directory listing is wanted.
Exercise
Nginx runs as www-data. The file /var/www/example.com/html/index.html has mode 644, yet sudo -u www-data cat on it fails with "Permission denied". What should you check, and with which command?
Show answer
Check the permissions of every parent directory with namei -l /var/www/example.com/html/index.html. One of the directories likely lacks the execute bit for others, so the worker user cannot traverse it. On an SELinux system, also check the file label with ls -Z and the mode with getenforce.