Bird
Raised Fist0
Nginxdevops~20 mins

Index 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
🎖️
Index Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What file does Nginx serve by default with this configuration?
Given the following Nginx configuration snippet, which file will Nginx serve when a user requests the root URL /?
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/html/default.html
C/var/www/html/index.php
D/var/www/html/home.html
Attempts:
2 left
💡 Hint
Look at the index directive to see which files Nginx tries first.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Nginx index directive
Which option shows the correct syntax for specifying multiple index files in Nginx?
Nginx
index index.html, index.htm index.php;
Aindex index.html, index.htm, index.php;
Bindex index.html index.htm index.php;
Cindex = index.html index.htm index.php;
Dindex: index.html index.htm index.php;
Attempts:
2 left
💡 Hint
Nginx directives separate values by spaces, not commas or symbols.
optimization
advanced
2:00remaining
Optimizing index directive for performance
Which index directive configuration is best for performance when your site primarily uses index.php and rarely index.html?
Aindex index.php index.html;
Bindex index.html index.php;
Cindex index.htm index.html index.php;
Dindex index.php;
Attempts:
2 left
💡 Hint
Put the most commonly used file first to reduce file checks.
🧠 Conceptual
advanced
2:00remaining
Effect of missing index directive
What happens if the index directive is not set in an Nginx server block and a user requests a directory URL?
ANginx returns a 404 Not Found error.
BNginx automatically serves <code>index.html</code> by default.
CNginx lists the directory contents to the user.
DNginx returns a 403 Forbidden error if no index file is found.
Attempts:
2 left
💡 Hint
Think about what Nginx does when no index file is found and directory listing is off.
🔧 Debug
expert
2:00remaining
Why does Nginx serve a 404 error despite correct index directive?
Given this configuration, why does Nginx return a 404 error when accessing /?
server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    index index.php index.html;
    location / {
        try_files $uri $uri/ =404;
    }
}
AThe <code>index</code> directive is ignored inside <code>location</code> blocks.
BThe root path is incorrect and does not contain index files.
CThe <code>try_files</code> directive does not check for index files inside directories.
DNginx requires <code>autoindex on;</code> to serve index files.
Attempts:
2 left
💡 Hint
Consider how try_files works with directories and index files.

Practice

(1/5)
1.

What is the main purpose of the index directive in nginx?

easy
A. To specify default files to serve when a directory is requested
B. To set the server's IP address
C. To configure SSL certificates
D. To define error pages

Solution

  1. Step 1: Understand the role of the index directive

    The index directive tells nginx which files to look for by default when a user requests a directory URL.
  2. Step 2: Match the purpose with the options

    Only To specify default files to serve when a directory is requested describes setting default files to serve in a folder, which matches the index directive's function.
  3. Final Answer:

    To specify default files to serve when a directory is requested -> Option A
  4. Quick Check:

    index directive = default files [OK]
Hint: Index sets default homepage files for folders [OK]
Common Mistakes:
  • Confusing index with server IP settings
  • Thinking index sets error pages
  • Mixing index with SSL configuration
2.

Which of the following is the correct syntax to set index.html and home.html as default files using the index directive?

?
easy
A. index: index.html; home.html;
B. index = index.html, home.html;
C. index { index.html home.html }
D. index index.html home.html;

Solution

  1. Step 1: Recall nginx index directive syntax

    The correct syntax lists files separated by spaces, ending with a semicolon.
  2. Step 2: Compare options with correct syntax

    index index.html home.html; matches the correct syntax: index index.html home.html;. Others use invalid punctuation or braces.
  3. Final Answer:

    index index.html home.html; -> Option D
  4. Quick Check:

    Correct syntax = index index.html home.html; [OK]
Hint: List files with spaces, end with semicolon [OK]
Common Mistakes:
  • Using commas between filenames
  • Using braces or colons incorrectly
  • Adding equal signs in directive
3.

Given this nginx config snippet:

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

If the folder contains index.html but not about.html, which file will nginx serve when a user visits /?

medium
A. 404 Not Found error
B. about.html
C. index.html
D. Directory listing

Solution

  1. Step 1: Understand index file priority

    nginx tries files in order: first about.html, then index.html.
  2. Step 2: Check which files exist

    about.html is missing, but index.html exists, so nginx serves index.html.
  3. Final Answer:

    index.html -> Option C
  4. Quick Check:

    First found file served = index.html [OK]
Hint: nginx serves first existing file in index list [OK]
Common Mistakes:
  • Assuming nginx serves first listed file regardless of existence
  • Expecting directory listing if first file missing
  • Thinking nginx returns error immediately
4.

Identify the error in this nginx config snippet:

location / {
    index index.html, home.html;
}
medium
A. Missing semicolon at the end
B. Using a comma between filenames is invalid syntax
C. index directive cannot be used inside location block
D. File names must be in quotes

Solution

  1. Step 1: Check syntax for index directive

    File names must be separated by spaces, not commas.
  2. Step 2: Identify the error in the snippet

    The comma between index.html and home.html is invalid syntax.
  3. Final Answer:

    Using a comma between filenames is invalid syntax -> Option B
  4. Quick Check:

    No commas allowed in index list [OK]
Hint: Separate files with spaces, no commas [OK]
Common Mistakes:
  • Adding commas between filenames
  • Omitting semicolon
  • Thinking quotes are required
5.

You want nginx to serve main.html as the default file, but only if index.html is missing. Which index directive correctly achieves this?

hard
A. index index.html main.html;
B. index main.html index.html;
C. index main.html;
D. index index.html;

Solution

  1. Step 1: Understand index file priority order

    nginx serves the first existing file in the list from left to right.
  2. Step 2: Choose order to serve main.html only if index.html missing

    To serve main.html only if index.html is missing, index.html must be first, then main.html.
  3. Final Answer:

    index index.html main.html; -> Option A
  4. Quick Check:

    First existing file served = index.html then main.html [OK]
Hint: List index.html first to prefer it over main.html [OK]
Common Mistakes:
  • Reversing file order
  • Listing only one file
  • Expecting nginx to skip files in order