0
0
Nginxdevops~5 mins

Root directive in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
The root directive in nginx tells the server where to find the files it should serve to users. It solves the problem of linking web addresses to actual files on the server.
When you want to serve static files like HTML, CSS, or images from a specific folder on your server.
When you need to set the main folder for your website content so nginx knows where to look.
When you want to host multiple websites on one server and each site has its own folder.
When you want to change the folder nginx uses without moving files around.
When you want to test a new version of your website by pointing nginx to a different folder.
Config File - nginx.conf
nginx.conf
events {}

http {
    server {
        listen 80;
        server_name example.com;

        root /var/www/example.com/html;

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

This configuration sets up a simple nginx server listening on port 80 for example.com.

The root directive tells nginx to look for files inside /var/www/example.com/html.

The location / block defines the main URL path and sets the default files to serve.

Commands
This command tests the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command fetches the homepage from the nginx server to verify it serves files from the root folder.
Terminal
curl http://example.com
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to example.com!</title> </head> <body> <h1>Success! The nginx server is working.</h1> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: the root directive tells nginx the exact folder where your website files live.

Common Mistakes
Setting root inside the location block but forgetting to set it in the server block.
nginx may not find files correctly because root is overridden or missing in some locations.
Set the root directive in the server block or explicitly in each location block where needed.
Using a relative path instead of an absolute path for root.
nginx requires an absolute path to find files; relative paths cause errors or no files served.
Always use the full absolute path starting from root, like /var/www/example.com/html.
Not reloading nginx after changing the root directive.
Changes won't take effect until nginx reloads the configuration.
Run 'sudo nginx -t' to test, then 'sudo systemctl reload nginx' to apply changes.
Summary
The root directive sets the folder where nginx looks for website files.
Test nginx configuration with 'nginx -t' before reloading.
Reload nginx to apply changes and verify by accessing the site.