Find the document root used by Nginx

Inspect the selected server and location instead of assuming a universal web-root directory across packages, containers, and source builds.

Best practices
Illustration of server operations and log analysis

Nginx has no universal public-web directory. The effective root or alias in the location serving a request determines its filesystem mapping. Package defaults are useful clues, but your configuration is the authority.

Inspect the correct instance

sudo nginx -T

Use the running service's binary and any custom -c or -p options. Read the relevant listen, server_name, and location blocks together. A search for every root line alone cannot tell you which one handles a particular hostname and path. -T reads files on disk, so also verify the last reload succeeded.

Common starting points include:

Installation Common document directory Configuration clue
Debian/Ubuntu packages /var/www/html Often sites-available/default, included through sites-enabled.
RPM-based packages Often /usr/share/nginx/html Main configuration or conf.d.
Official Nginx container image /usr/share/nginx/html Commonly /etc/nginx/conf.d/default.conf.
Source build <prefix>/html Often <prefix>/conf/nginx.conf.

Custom packaging can change any of these. The Nginx directive's default is relative html, resolved using the configured prefix; a source build does not inherently use /usr/share/nginx/html.

Read a simple static server

server {
    listen 80 default_server;
    server_name _;
    root /var/www/html;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

For this server, /images/logo.png maps to /var/www/html/images/logo.png. A request by IP selects the applicable default server unless another name/address rule applies. The welcome page is just a file selected by that configuration, not a special built-in response.

Change the site directory

Set the intended root, ensure the files exist, then test and reload. The worker needs search permission on every parent directory and read access to the served files. Modes such as 755 for public directories and 644 for public files are conventional, but groups or ACLs can grant narrower access.

On SELinux systems, a nonstandard public-content location may need a persistent label mapping:

sudo semanage fcontext -a -t httpd_sys_content_t '/srv/site(/.*)?'
sudo restorecon -Rv /srv/site
sudo nginx -t && sudo systemctl reload nginx

Modify an existing mapping rather than adding a duplicate. Check a known file and the precise logged path if it fails. Guance Nginx collection can retain errors, but a 403 or 404 alone does not identify the cause.

FAQ

Can I delete the default page?

Yes, after confirming that it is only an unwanted placeholder. Supply the intended index or application handler. Otherwise a directory request may return a directory-index error, depending on the remaining configuration.

How should multiple sites be organized?

Separate directories and server blocks often make ownership and deployment clearer. Subpath hosting is also valid when the URL and filesystem mappings are intentional; it is not inherently wrong.

How does alias differ from root?

root appends the URI. A prefix alias replaces the matched location prefix. For location /images/, root /data; maps /images/a.png to /data/images/a.png, while alias /data/pictures/; maps it to /data/pictures/a.png. Regex aliases need capture-based mapping.

References: Nginx root and alias, official container source.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting