Bird
Raised Fist0
Nginxdevops~5 mins

Directory listing (autoindex) in Nginx - Cheat Sheet & Quick Revision

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
Recall & Review
beginner
What does the autoindex directive do in nginx?
The autoindex directive enables or disables automatic directory listing when no index file is found in a directory.
Click to reveal answer
beginner
How do you enable directory listing for a specific location in nginx?
Inside the location block, set autoindex on; to enable directory listing for that path.
Click to reveal answer
beginner
What is the default state of autoindex in nginx?
By default, autoindex is off, so directory listing is disabled unless explicitly enabled.
Click to reveal answer
beginner
Which nginx configuration block is used to control directory listing?
The location block controls directory listing by using the autoindex directive inside it.
Click to reveal answer
intermediate
What happens if autoindex is off and no index file exists in a directory?
nginx will return a 403 Forbidden or 404 Not Found error instead of showing the directory contents.
Click to reveal answer
Which directive enables directory listing in nginx?
Adirlist enable;
Bautoindex on;
Clistdir on;
Dindex on;
Where do you place the autoindex directive to enable directory listing?
AInside the <code>server</code> block only
BInside the <code>http</code> block only
CInside the <code>location</code> block
DIn the <code>events</code> block
What is the default value of autoindex in nginx?
Atrue
Bon
Cenabled
Doff
If directory listing is disabled and no index file exists, what does nginx do?
AReturns 403 or 404 error
BRedirects to homepage
CShows directory contents
DAutomatically creates an index file
How do you disable directory listing if it was previously enabled?
ASet <code>autoindex off;</code> in the location block
BRemove the <code>location</code> block
CSet <code>index off;</code>
DRestart nginx without changes
Explain how to enable directory listing in nginx and what configuration block is used.
Think about where nginx controls URL paths and how to turn on directory listing.
You got /4 concepts.
    Describe what happens when directory listing is disabled and a user tries to access a directory without an index file.
    Consider nginx's default behavior when it cannot find a file to serve.
    You got /4 concepts.

      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