1. Introduction

The NGINX server is a common Web server that can serve as a forward and reverse proxy, as well as a load balancer and mail proxy. Its configuration file nginx.conf can be in several locations by default:

  • /etc/nginx
  • /usr/local/nginx/conf
  • /usr/local/etc/nginx

In fact, the nginx.conf file provides a way to set options and parameters of the server, including the location of the document root, where all the web server data resides. However, there are other conditions that augment this location.

In this tutorial, we explore ways to check and set the document root and its default value. First, we discuss how to set the default document root path. After that, we check ways to infer that from the compile-time, installation, and runtime options.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Document Root Path Configuration

As usual, we can set the default document root in the nginx.conf configuration file:

server {
    root /dir/subdir;

On the one hand, we can place the root directive directly in the server block. On the other hand, we can place it within a location block. Either way, we can use variables within the path as well.

The value of a root directive in the block of the virtual server we’re accessing is always used. If its value is a relative path, that gets added to any prefixes we explore later. Absolute paths override other means of setting the document root.

Without an explicit root directive, the default document root becomes the relative path html.

The absolute prefix of this relative path depends on the default compile-time, installation, and runtime paths, just like the location of nginx.conf itself.

3. Check Default Paths

Since it’s not part of the NGINX code, the global default path and prefix depend on the compilation options, installation parameters, and the way we run the final binary. In turn, this global default can determine the default web root path.

Let’s see how the former changes depending on the context.

3.1. configure

Like most open-source software, we can leverage the parameters of the NGINX configure script to modify settings:

$ ./configure [...] --prefix=/dir/subdir

In this case, we set the –prefix argument to /dir/subdir. The default configure –prefix value is /usr/local/nginx, so the final absolute document root path html becomes /usr/local/nginx/html. Of course, if the root directive points at another relative path, that gets added instead.

To check the –prefix value of our current installation, we can use the nginx binary:

$ nginx -V
nginx version: nginx/1.18.0
built with OpenSSL 1.1.1n  15 Mar 2022
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-x3gsRV/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf[...]

Here, the last two values we see are for the –prefix and –conf-path parameters. The latter specifies the default configuration file, also important when setting the document root path, as we already saw.

3.2. nginx -p

In addition, we can supply a value to the -p flag of the nginx binary itself:

$ nginx -p /dir/subdir

This way, we override the directory set by configure. Here, the default -p value of nginx is /usr/share/nginx, so the absolute document root path would be /usr/share/nginx/html.

3.3. Path Specifics

Critically, most settings depend on the way NGINX is packaged. If not manually building from the official NGINX repository, we have to investigate the used NGINX package we to know the exact parameter values it sets.

For example, Debian uses /var/www/html for the nginx package, just like with apache. Yet, the Debian-based Ubuntu defaults to /usr/share/nginx/www. However, this is not part of the nginx -V output.

4. Summary

In this article, we explored ways to check and set the default and current document root path.

In conclusion, depending on our needs, we can change the global path prefix or only modify the document root for a particular virtual server.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.