Nginx and Apache default paths

A comprehensive guide to understanding and configuring document root paths in Apache HTTP Server and NGINX web servers.


Understanding Document Root

The document root is the directory on your server where web content files are stored and served from. When a web server receives a request, it maps the requested URL to a file path by combining the document root with the requested URI.

Example: If your document root is /var/www/html and a user requests http://example.com/about.html, the server will serve the file located at /var/www/html/about.html.


Apache HTTP Server 2.4

DocumentRoot Directive

ℹ️

Source: Apache HTTP Server Version 2.4 Official Documentation
http://httpd.apache.org/docs/2.4/

The DocumentRoot directive specifies where in your filesystem you should place web content files.

How it works: Apache's default behavior is to take the URL-Path for the request (the part of the URL following the hostname and port) and add it to the end of the DocumentRoot specified in your configuration files.

Common Default Paths

Red Hat Enterprise Linux (RHEL):

  • Default DocumentRoot: /var/www/html
  • Main configuration file: /etc/httpd/conf/httpd.conf

Source: Red Hat Enterprise Linux 4 Reference Guide states: "The default DocumentRoot, for both the non-secure and secure Web servers, is the /var/www/html directory."

From-Source Installation:

  • If you installed httpd from source, the default location of the configuration files is /usr/local/apache2/conf
  • The default configuration file is usually called httpd.conf

Configuration File Locations

According to the Apache HTTP Server Getting Started guide:

"The Apache HTTP Server is configured via simple text files. These files may be located any of a variety of places, depending on how exactly you installed the server."

"If you installed httpd from source, the default location of the configuration files is /usr/local/apache2/conf. The default configuration file is usually called httpd.conf."

The configuration is frequently broken into multiple smaller files for ease of management, loaded via the Include directive.

DocumentRoot Examples

Example 1: Basic DocumentRoot

DocumentRoot "/var/www/html"

If DocumentRoot is set to /var/www/html, a request for http://www.example.com/fish/guppies.html would result in the file /var/www/html/fish/guppies.html being served to the requesting client.

Example 2: Virtual Host DocumentRoot

<VirtualHost *:80>
    DocumentRoot "/www/example1"
    ServerName www.example.com
    # Other directives here
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/example2"
    ServerName www.example.org
    # Other directives here
</VirtualHost>

Directory Index Files

ℹ️

Typically, a document called index.html will be served when a directory is requested without a file name being specified. For example, if DocumentRoot is set to /var/www/html and a request is made for http://www.example.com/work/, the file /var/www/html/work/index.html will be served to the client.

Virtual Hosting

Apache is capable of Virtual Hosting, where the server receives requests for more than one host. In this case, a different DocumentRoot can be specified for each virtual host.

The DocumentRoot directive is set in your main server configuration file (httpd.conf) and, possibly, once per additional Virtual Host you create.


NGINX

Root Directive

ℹ️

Source: NGINX Official Documentation
http://nginx.org/en/docs/

The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive.

Common Default Paths

Compilation from Source:

  • Default configuration file: /usr/local/nginx/conf/nginx.conf
  • Default process ID file: /usr/local/nginx/logs/nginx.pid

Common Package Installation Paths:

  • Configuration directory: /etc/nginx or /usr/local/etc/nginx
  • Configuration file: /etc/nginx/nginx.conf

Default Document Root (when using relative path html):

  • With default compile-time prefix: /usr/local/nginx/html
  • Varies based on compilation options and installation parameters

How the Root Directive Works

According to NGINX's Beginner's Guide:

"This location block specifies the '/' prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system."

Example: If root is set to /data/www, a request for /logo.gif is mapped to the file /data/www/logo.gif.

Root Directive Contexts

The root directive can be placed on any level within the http {}, server {}, or location {} contexts.

Configuration Examples

Example 1: Server-Level Root

server {
    root /www/data;

    location / {
    }

    location /images/ {
    }

    location ~ \.(mp3|mp4) {
        root /www/media;
    }
}

In this example:

  • NGINX searches for a URI that starts with /images/ in the /www/data/images/ directory
  • If the URI ends with .mp3 or .mp4 extension, NGINX searches in the /www/media/ directory (defined in the matching location block)

Example 2: Complete Server Block

server {
    listen 80;
    server_name example.org www.example.org;
    root /data/www;

    location / {
        index index.html index.php;
    }

    location ~* \.(gif|jpg|png)$ {
        expires 30d;
    }

    location ~ \.php$ {
        fastcgi_pass localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Example 3: Red Hat Enterprise Linux 9 Default Configuration

According to Red Hat Enterprise Linux 9 documentation:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    root /usr/share/nginx/html;
}

The server_name parameter set to _ configures NGINX to accept any host name for this server block.

Configuration File Management

From the NGINX Beginner's Guide:

"By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx."

Testing Configuration

Before applying changes, test the configuration syntax:

nginx -t

Reloading Configuration

After making configuration changes, reload NGINX to apply them:

nginx -s reload

According to the official documentation:

"Changes made in the configuration file will not be applied until the command to reload configuration is sent to nginx or it is restarted."


Key Differences: Apache vs NGINX

AspectApacheNGINX
Directive NameDocumentRootroot
Path ConstructionDocumentRoot + URIroot + URI
Default (from source)/usr/local/apache2/conf (config location)/usr/local/nginx/conf (config location)
Config File Namehttpd.confnginx.conf
Virtual Host Syntax<VirtualHost> blocksserver blocks
Config Test Commandapachectl configtestnginx -t
Reload Commandapachectl gracefulnginx -s reload

Testing Your Configuration

Apache Testing

Verify configuration syntax:

apachectl configtest

Expected output: Syntax OK

NGINX Testing

Verify configuration syntax:

nginx -t

Expected output similar to:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Creating Test Files

Apache Test:

# Create test HTML file
echo "<h1>Apache is working</h1>" > /var/www/html/test.html

# Access in browser
http://your-server-ip/test.html

NGINX Test:

# Create test HTML file (adjust path based on your root)
echo "<h1>NGINX is working</h1>" > /usr/share/nginx/html/test.html

# Access in browser
http://your-server-ip/test.html

Important Variables

Apache Variables

  • DocumentRoot: The directory configured as the document root
  • Configuration is set in httpd.conf or virtual host files

NGINX Variables

According to the ngx_http_core_module documentation:

  • $document_root: An absolute pathname corresponding to the root or alias directive's value for the current request, with all symbolic links resolved to real paths

Example usage in configuration:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Additional Considerations

File Not Found Errors

⚠️

Apache: URLs will be requested for which no matching file can be found in the filesystem. This can be a result of moving documents from one location to another, or accidental mistyping of URLs. Apache provides the module mod_speling (sic) to help with this problem.

NGINX: If no location matches or no file exists, NGINX returns an error page with HTTP status code 404 (file not found).

Security Considerations

Both Apache and NGINX require proper file permissions and directory access controls to prevent unauthorized access to files outside the document root.

Symbolic Links

Apache: On Unix systems, symbolic links can bring other parts of the filesystem under the DocumentRoot. For security reasons, httpd will follow symbolic links only if the Options setting for the relevant directory includes FollowSymLinks or SymLinksIfOwnerMatch.

NGINX: Automatically resolves symbolic links when determining the document root path.


Quick Reference

Apache Default Paths Summary

ItemPath
RHEL DocumentRoot/var/www/html
RHEL Config File/etc/httpd/conf/httpd.conf
Source Install Config/usr/local/apache2/conf/httpd.conf
Default Index Fileindex.html

NGINX Default Paths Summary

ItemPath
Common Config File/etc/nginx/nginx.conf
Source Install Config/usr/local/nginx/conf/nginx.conf
RHEL 9 Default Root/usr/share/nginx/html
Source Install Root (default)/usr/local/nginx/html

References

All information in this document is sourced from official vendor documentation:

Apache HTTP Server:

NGINX: