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 calledhttpd.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.htmlwill be served when a directory is requested without a file name being specified. For example, if DocumentRoot is set to/var/www/htmland a request is made forhttp://www.example.com/work/, the file/var/www/html/work/index.htmlwill 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/nginxor/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
.mp3or.mp4extension, 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.confand 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 -tReloading Configuration
After making configuration changes, reload NGINX to apply them:
nginx -s reloadAccording 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
| Aspect | Apache | NGINX |
|---|---|---|
| Directive Name | DocumentRoot | root |
| Path Construction | DocumentRoot + URI | root + URI |
| Default (from source) | /usr/local/apache2/conf (config location) | /usr/local/nginx/conf (config location) |
| Config File Name | httpd.conf | nginx.conf |
| Virtual Host Syntax | <VirtualHost> blocks | server blocks |
| Config Test Command | apachectl configtest | nginx -t |
| Reload Command | apachectl graceful | nginx -s reload |
Testing Your Configuration
Apache Testing
Verify configuration syntax:
apachectl configtestExpected output: Syntax OK
NGINX Testing
Verify configuration syntax:
nginx -tExpected 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.htmlNGINX 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.htmlImportant Variables
Apache Variables
DocumentRoot: The directory configured as the document root- Configuration is set in
httpd.confor 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
| Item | Path |
|---|---|
| RHEL DocumentRoot | /var/www/html |
| RHEL Config File | /etc/httpd/conf/httpd.conf |
| Source Install Config | /usr/local/apache2/conf/httpd.conf |
| Default Index File | index.html |
NGINX Default Paths Summary
| Item | Path |
|---|---|
| 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:
- Apache HTTP Server Version 2.4 Documentation: http://httpd.apache.org/docs/2.4/
- Getting Started Guide: http://httpd.apache.org/docs/current/getting-started.html
- URL Mapping Documentation: http://httpd.apache.org/docs/2.4/urlmapping.html
- Virtual Host Examples: http://httpd.apache.org/docs/2.4/vhosts/examples.html
- Red Hat Enterprise Linux 4 Reference Guide (DocumentRoot): https://docs.redhat.com/
NGINX:
- NGINX Official Documentation: http://nginx.org/en/docs/
- Beginner's Guide: http://nginx.org/en/docs/beginners_guide.html
- Serving Static Content: https://docs.nginx.com/nginx/admin-guide/web-server/serving-static-content/
- ngx_http_core_module: http://nginx.org/en/docs/http/ngx_http_core_module.html
- Request Processing: http://nginx.org/en/docs/http/request_processing.html
- Red Hat Enterprise Linux 9 NGINX Documentation: https://docs.redhat.com/
Updated 18 days ago