Bird
Raised Fist0
Nginxdevops~10 mins

Directory listing (autoindex) in Nginx - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to enable directory listing in an Nginx server block.

Nginx
location /files/ {
    [1] on;
}
Drag options to blanks, or click blank then click option'
Alisten
Bindex
Croot
Dautoindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' instead of 'autoindex' disables directory listing.
Using 'root' or 'listen' does not control directory listing.
2fill in blank
medium

Complete the code to set the directory listing style to 'exact_size' in Nginx.

Nginx
location /downloads/ {
    autoindex on;
    autoindex_format [1];
}
Drag options to blanks, or click blank then click option'
Aexact_size
Bplain
Chtml
Djson
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'html' shows sizes in human-readable format, not exact bytes.
Using unsupported formats like 'json' or 'plain' causes errors.
3fill in blank
hard

Fix the error in the Nginx config to enable directory listing inside the location block.

Nginx
location /media/ {
    autoindex [1];
}
Drag options to blanks, or click blank then click option'
Aenable
Btrue
Con
Dyes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'enable' instead of 'on' causes syntax errors.
Using 'true' or 'yes' is invalid in Nginx configs.
4fill in blank
hard

Fill both blanks to configure Nginx to serve directory listing with exact file sizes.

Nginx
location /public/ {
    autoindex [1];
    autoindex_format [2];
}
Drag options to blanks, or click blank then click option'
Aon
Boff
Cexact_size
Dhidden
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autoindex to 'off' disables directory listing.
Using wrong format values causes errors.
5fill in blank
hard

Fill both blanks to create a location block that enables directory listing and sets format to HTML.

Nginx
location /assets/ {
    autoindex [1];
    autoindex_format [2];
}
Drag options to blanks, or click blank then click option'
Aoff
Bon
Chtml
Dhidden
Attempts:
3 left
💡 Hint
Common Mistakes
Turning autoindex off disables listing.
Using 'exact_size' instead of 'html' changes the size display style.

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