0
0
Nginxdevops~20 mins

Root directive in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Root Directive Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of accessing the root URL?

Given this nginx server block configuration, what file will nginx serve when a user accesses http://example.com/?

Nginx
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.html index.htm;
}
A/var/www/html/index.html
B/var/www/index.html
C/var/www/html/default.html
D/var/www/html/home.html
Attempts:
2 left
💡 Hint

Check the root and index directives to find the default file served.

Configuration
intermediate
2:00remaining
Identify the correct root directive placement

Where should the root directive be placed to serve files correctly for a specific location?

Nginx
server {
    listen 80;
    server_name example.com;

    location /images/ {
        # Where to place root?
    }
}
AInside the <code>location /images/</code> block
BOnly inside the <code>server</code> block, not inside <code>location</code>
COutside both <code>server</code> and <code>location</code> blocks
DInside the <code>http</code> block only
Attempts:
2 left
💡 Hint

Think about how nginx uses root for different URL paths.

Troubleshoot
advanced
2:00remaining
Why does nginx return 404 for a valid file?

Given this configuration, nginx returns 404 when accessing http://example.com/images/pic.jpg even though the file exists at /var/www/images/pic.jpg. What is the cause?

Nginx
server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    location /images/ {
        root /var/www/images;
    }
}
ABecause the root directive inside location is ignored, so nginx looks in /var/www/html/images/pic.jpg
BBecause nginx appends the location URI to the root, resulting in /var/www/images/images/pic.jpg which does not exist
CBecause the file permissions on /var/www/images/pic.jpg are incorrect
DBecause the server_name does not match the request
Attempts:
2 left
💡 Hint

Remember how nginx combines root and the request URI inside location.

Best Practice
advanced
2:00remaining
Choose the best way to serve static files from a subdirectory

You want to serve static files from /var/www/static when users access /static/ URL. Which configuration is best?

Alocation /static/ { root /var/www; }
Blocation /static/ { root /var/www/static; }
Clocation /static/ { alias /var/www/static/; }
Dlocation /static/ { proxy_pass http://localhost/static/; }
Attempts:
2 left
💡 Hint

Consider how root and alias handle URI paths differently.

🧠 Conceptual
expert
3:00remaining
How does nginx resolve the root path with nested locations?

Consider this nginx configuration:

server {
    listen 80;
    server_name example.com;
    root /var/www/html;

    location / {
        root /var/www/site;

        location /images/ {
            root /var/www/images_root;
        }
    }
}

What is the full filesystem path nginx will use to serve http://example.com/images/pic.jpg?

A/var/www/html/images/pic.jpg
B/var/www/images_root/pic.jpg
C/var/www/site/images/pic.jpg
D/var/www/images_root/images/pic.jpg
Attempts:
2 left
💡 Hint

Remember that nested location blocks override the root directive independently.