Bird
Raised Fist0
Nginxdevops~3 mins

Why Directory listing (autoindex) in Nginx? - Purpose & Use Cases

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
The Big Idea

What if your server could list all your files for you, instantly and perfectly every time?

The Scenario

Imagine you have a folder full of files on your web server, and you want to share them quickly with friends or colleagues.

Without any special setup, you have to create a webpage listing all those files manually or send each file link one by one.

The Problem

Manually updating a list of files every time you add or remove one is slow and boring.

You might forget to update the list, causing confusion or broken links.

This wastes time and can frustrate everyone involved.

The Solution

Directory listing (autoindex) in nginx automatically shows all files in a folder as a neat list on a webpage.

It updates instantly when files change, so you never have to do it yourself.

Before vs After
Before
Create index.html with links to each file
Update index.html every time files change
After
location /files/ {
    autoindex on;
}
What It Enables

You can instantly share and browse files on your server without extra work or mistakes.

Real Life Example

A photographer shares a folder of photos with clients by enabling autoindex, so clients can see and download all pictures easily.

Key Takeaways

Manual file listing is slow and error-prone.

Autoindex automatically shows current files in a directory.

This saves time and makes file sharing simple and reliable.

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