Bird
Raised Fist0
Nginxdevops~20 mins

Root directive in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does the root directive do in an nginx configuration?
easy
A. It sets the maximum number of client connections.
B. It defines the server's IP address.
C. It specifies the port number nginx listens on.
D. It sets the folder where nginx looks for files to serve.

Solution

  1. Step 1: Understand the purpose of the root directive

    The root directive tells nginx which folder to use as the base path for serving files.
  2. Step 2: Compare with other options

    Other options like IP address, port, or client limits are set by different directives, not root.
  3. Final Answer:

    It sets the folder where nginx looks for files to serve. -> Option D
  4. Quick Check:

    root = folder path for files [OK]
Hint: Root sets the base folder for files nginx serves [OK]
Common Mistakes:
  • Confusing root with listen directive
  • Thinking root sets server IP or port
  • Assuming root controls client limits
2. Which of the following is the correct syntax to set the root directory to /var/www/html inside a location block?
easy
A. root = /var/www/html;
B. root /var/www/html;
C. root: /var/www/html;
D. root /var/www/html

Solution

  1. Step 1: Recall nginx directive syntax

    Directives use the format: directive_name value; without equals or colons.
  2. Step 2: Check each option

    root /var/www/html; uses correct syntax: root /var/www/html;. Options A and C use invalid symbols, D misses the semicolon.
  3. Final Answer:

    root /var/www/html; -> Option B
  4. Quick Check:

    Correct syntax = root path followed by semicolon [OK]
Hint: No equals sign or colon; end with semicolon [OK]
Common Mistakes:
  • Using equals sign (=) after root
  • Omitting semicolon at end
  • Using colon (:) instead of space
3. Given this nginx config snippet inside a server block:
location /images/ {
    root /var/www/data;
}

If a client requests /images/pic.jpg, which file path will nginx try to serve?
medium
A. /var/www/data/images/pic.jpg
B. /var/www/data/pic.jpg
C. /images/pic.jpg
D. /var/www/html/images/pic.jpg

Solution

  1. Step 1: Understand root with location

    The root directive appends the full request URI to the root path.
  2. Step 2: Combine root and request URI

    Request URI is /images/pic.jpg. Root is /var/www/data. So nginx looks for /var/www/data/images/pic.jpg.
  3. Final Answer:

    /var/www/data/images/pic.jpg -> Option A
  4. Quick Check:

    root + full URI = file path [OK]
Hint: Root + full URI = file path nginx serves [OK]
Common Mistakes:
  • Assuming root replaces location prefix
  • Ignoring location path in file path
  • Confusing root with alias directive
4. You set root /var/www/html; inside a location /static/ block, but requests to /static/style.css return 404 errors. What is the most likely cause?
medium
A. The root path is incorrect or missing the /static/ folder.
B. You forgot to restart nginx after changing the config.
C. The location block should use alias instead of root.
D. The file style.css is not readable by nginx.

Solution

  1. Step 1: Understand root with location prefix

    Root appends the full URI, so nginx looks for /var/www/html/static/style.css.
  2. Step 2: Check folder structure

    If the actual files are in /var/www/html without the static subfolder, nginx won't find them, causing 404.
  3. Final Answer:

    The root path is incorrect or missing the /static/ folder. -> Option A
  4. Quick Check:

    Root + URI must match actual file path [OK]
Hint: Root must include location path or use alias [OK]
Common Mistakes:
  • Not matching root path to location URI
  • Assuming config reload fixes path errors
  • Confusing root and alias usage
5. You want nginx to serve files from /srv/www/site when users request /files/, but without including /files/ in the file path. Which configuration correctly achieves this?
hard
A. location /files/ { root /srv/www/site; }
B. location /files/ { alias /srv/www/site/files; }
C. location /files/ { alias /srv/www/site/; }
D. location /files/ { root /srv/www/site/files; }

Solution

  1. Step 1: Understand root vs alias behavior

    Root appends full URI to path, alias replaces location prefix with given path.
  2. Step 2: Match requirement to config

    To serve files from /srv/www/site without /files/ in path, alias must be used.
  3. Final Answer:

    location /files/ { alias /srv/www/site/; } -> Option C
  4. Quick Check:

    Use alias to strip location prefix from file path [OK]
Hint: Use alias to avoid adding location prefix to path [OK]
Common Mistakes:
  • Using root when alias is needed
  • Missing trailing slash in alias path
  • Confusing root and alias effects