Bird
Raised Fist0
Nginxdevops~20 mins

Directory listing (autoindex) 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
🎖️
Directory Listing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this nginx configuration snippet?
Given this nginx server block snippet, what will be the result when accessing the /files URL?
location /files {
    root /var/www/html;
    autoindex on;
}
AThe browser shows a 404 Not Found error because the root directive is incorrect.
BA plain text list of files and directories inside /var/www/html/files is shown in the browser.
CThe browser downloads the index.html file from /var/www/html/files automatically.
DThe browser shows a 403 Forbidden error because autoindex is off by default.
Attempts:
2 left
💡 Hint
autoindex on enables directory listing if no index file is found.
Configuration
intermediate
2:00remaining
Which configuration enables directory listing only for /public path?
Select the correct nginx configuration snippet that enables directory listing only for the /public URL path.
A
location /public {
    root /var/www/html;
    autoindex on;
}
B
location / {
    root /var/www/html/public;
    autoindex on;
}
C
location /public {
    root /var/www/html/public;
    autoindex off;
}
D
location /public {
    alias /var/www/html/public;
    autoindex off;
}
Attempts:
2 left
💡 Hint
autoindex must be on and root must point to the base directory.
Troubleshoot
advanced
2:00remaining
Why does directory listing not show despite autoindex on?
You set autoindex on in your nginx config for /data, but visiting /data shows a 403 Forbidden error. What is the most likely cause?
AThe index directive is set to a file that does not exist, causing 403.
BThe autoindex directive must be set inside the http block, not location.
CThe root directive is missing, so nginx cannot find the directory.
DThe directory /var/www/html/data lacks read permissions for the nginx user.
Attempts:
2 left
💡 Hint
Check file system permissions for nginx user.
Best Practice
advanced
2:00remaining
What is the safest way to enable directory listing for a public folder?
Choose the best practice to safely enable directory listing for /public folder in nginx.
AEnable autoindex on only for /public location and restrict access to other locations.
BUse autoindex on in /public and disable access logging to hide activity.
CEnable autoindex on globally in the http block for all locations.
DEnable autoindex on and disable all authentication to allow anonymous access.
Attempts:
2 left
💡 Hint
Limit directory listing to only needed paths.
🔀 Workflow
expert
3:00remaining
Order the steps to enable and verify directory listing in nginx
Put these steps in the correct order to enable directory listing for /files and verify it works.
A1,3,2,4
B1,2,3,4
C3,1,2,4
D3,2,1,4
Attempts:
2 left
💡 Hint
Check permissions before reloading nginx.

Practice

(1/5)
1. What does the autoindex on; directive do in an nginx server block?
easy
A. It enables directory listing so users can see files in a folder via browser.
B. It disables access to all files in the directory.
C. It compresses files before sending to the client.
D. It redirects requests to another server.

Solution

  1. Step 1: Understand the purpose of autoindex

    The autoindex directive controls whether nginx shows a list of files in a directory when no index file is found.
  2. Step 2: Effect of autoindex on;

    Setting it to on enables directory listing, allowing users to browse files via a web browser.
  3. Final Answer:

    It enables directory listing so users can see files in a folder via browser. -> Option A
  4. Quick Check:

    autoindex on = directory listing enabled [OK]
Hint: autoindex on means show files in folder via browser [OK]
Common Mistakes:
  • Thinking autoindex disables access
  • Confusing autoindex with compression
  • Assuming autoindex redirects requests
2. Which of the following is the correct syntax to enable directory listing in nginx?
easy
A. autoindex enable;
B. autoindex on;
C. autoindex true;
D. autoindex yes;

Solution

  1. Step 1: Recall nginx directive syntax

    nginx directives use specific keywords; for enabling autoindex, the keyword is on.
  2. Step 2: Identify correct keyword

    Only autoindex on; is valid syntax. Others like enable, true, or yes are invalid.
  3. Final Answer:

    autoindex on; -> Option B
  4. Quick Check:

    Correct syntax = autoindex on; [OK]
Hint: Use 'on' to enable autoindex, not 'enable' or 'yes' [OK]
Common Mistakes:
  • Using 'enable' instead of 'on'
  • Using 'yes' or 'true' which are invalid
  • Missing semicolon at end
3. Given this nginx config snippet inside a server block:
location /files/ {
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}

What will the directory listing show regarding file sizes and timestamps?
medium
A. File sizes in human-readable format and timestamps in local time.
B. File sizes in bytes and timestamps in UTC time.
C. File sizes hidden and timestamps not shown.
D. File sizes in human-readable format and timestamps in UTC time.

Solution

  1. Step 1: Understand autoindex_exact_size off;

    This setting shows file sizes in human-readable format (e.g., KB, MB) instead of bytes.
  2. Step 2: Understand autoindex_localtime on;

    This setting shows file timestamps in the server's local time zone, not UTC.
  3. Final Answer:

    File sizes in human-readable format and timestamps in local time. -> Option A
  4. Quick Check:

    autoindex_exact_size off + autoindex_localtime on = human sizes + local time [OK]
Hint: Exact size off = human sizes; localtime on = local timestamps [OK]
Common Mistakes:
  • Assuming sizes always show in bytes
  • Thinking timestamps are always UTC
  • Confusing off/on meaning for these directives
4. You enabled autoindex on; but directory listing still does not show. Which is the most likely cause?
medium
A. The server block is missing the listen directive.
B. The autoindex directive is misspelled as autoindx.
C. The directory has an index.html file present.
D. The autoindex directive must be inside http block only.

Solution

  1. Step 1: Check nginx directory listing behavior

    nginx shows directory listing only if no index file (like index.html) exists in the directory.
  2. Step 2: Identify why listing is not shown

    If an index file is present, nginx serves it instead of showing directory listing, even if autoindex on; is set.
  3. Final Answer:

    The directory has an index.html file present. -> Option C
  4. Quick Check:

    Index file present blocks directory listing [OK]
Hint: Index files override autoindex directory listing [OK]
Common Mistakes:
  • Assuming misspelling causes no effect (it causes error)
  • Thinking autoindex must be in http block only
  • Ignoring presence of index files
5. You want to allow users to browse files in /var/www/public via nginx with directory listing enabled, showing file sizes in human-readable format and timestamps in local time. Which configuration snippet inside the server block is correct?
hard
A. location /public/ { root /var/www/public; autoindex on; autoindex_exact_size on; autoindex_localtime off; }
B. location /public/ { root /var/www; autoindex off; autoindex_exact_size off; autoindex_localtime on; }
C. location /public/ { alias /var/www/public; autoindex off; autoindex_exact_size off; autoindex_localtime on; }
D. location /public/ { root /var/www; autoindex on; autoindex_exact_size off; autoindex_localtime on; }

Solution

  1. Step 1: Choose correct root and enable autoindex

    Using root /var/www; with location /public/ serves files from /var/www/public. autoindex on; enables directory listing.
  2. Step 2: Set human-readable sizes and local timestamps

    autoindex_exact_size off; shows sizes in human-readable format, and autoindex_localtime on; shows timestamps in local time.
  3. Final Answer:

    location /public/ { root /var/www; autoindex on; autoindex_exact_size off; autoindex_localtime on; } -> Option D
  4. Quick Check:

    Correct root + autoindex on + human sizes + local time = location /public/ { root /var/www; autoindex on; autoindex_exact_size off; autoindex_localtime on; } [OK]
Hint: Use root with path prefix; autoindex on + exact_size off + localtime on [OK]
Common Mistakes:
  • Using alias incorrectly with root paths
  • Turning autoindex off disables listing
  • Setting exact_size on shows bytes, not human sizes