0
0
Nginxdevops~10 mins

Root directive in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Root directive
Start nginx config parsing
Find server block
Look for root directive
Set root path for requests
Receive HTTP request
Combine root path + URI
Serve file from combined path
Send response to client
The root directive sets the base folder for serving files. When a request comes, nginx combines this root path with the request URI to find the file to serve.
Execution Sample
Nginx
server {
    listen 80;
    root /var/www/html;
}
This config sets the root folder to /var/www/html for all requests on port 80.
Process Table
StepActionValue/ResultNotes
1Parse server blockFound root directiveroot = /var/www/html
2Receive HTTP requestGET /index.htmlClient requests /index.html
3Combine root + URI/var/www/html/index.htmlFull file path to serve
4Check file existenceFile existsFile found at path
5Serve fileSend contents of /var/www/html/index.htmlResponse sent to client
6End requestRequest completeCycle finished
💡 Request served successfully using root path combined with URI
Status Tracker
VariableStartAfter Step 2After Step 3Final
rootundefined/var/www/html/var/www/html/var/www/html
request_uriundefined/index.html/index.html/index.html
full_pathundefinedundefined/var/www/html/index.html/var/www/html/index.html
Key Moments - 2 Insights
Why does nginx combine the root path with the request URI?
Because the root directive only sets the base folder. To find the exact file to serve, nginx adds the requested URI path to this base folder, as shown in step 3 of the execution table.
What happens if the root directive is missing?
Nginx won't know where to find files to serve, so requests may fail. The execution table starts with finding the root directive (step 1), which is essential for serving files.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the full file path nginx tries to serve at step 3?
A/var/www/index.html
B/index.html
C/var/www/html/index.html
D/html/index.html
💡 Hint
Check the 'Combine root + URI' action in step 3 of the execution table.
At which step does nginx confirm the requested file exists?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for the 'Check file existence' action in the execution table.
If the root directive was changed to /usr/share/nginx/html, what would be the full path at step 3?
A/var/www/html/index.html
B/usr/share/nginx/html/index.html
C/usr/share/index.html
D/nginx/html/index.html
💡 Hint
Refer to the variable_tracker for 'root' and how it combines with 'request_uri'.
Concept Snapshot
root directive syntax:
root /path/to/folder;

Sets the base folder for serving files.
Nginx combines root + request URI to find files.
Missing root causes file serving failure.
Used inside server or location blocks.
Full Transcript
The root directive in nginx sets the base folder where files are served from. When nginx receives a request, it takes the root path and adds the requested URI to form the full file path. For example, if root is /var/www/html and the request is /index.html, nginx looks for /var/www/html/index.html. The execution table shows parsing the config, receiving the request, combining paths, checking file existence, and serving the file. Variables like root, request_uri, and full_path change as the request is processed. Understanding this flow helps avoid common mistakes like missing root or wrong paths.