Directory listing (autoindex) in Nginx - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When nginx shows a directory listing, it reads the folder contents to display them. We want to understand how the time it takes grows as the number of files increases.
How does the work needed change when there are more files in the directory?
Analyze the time complexity of the following nginx configuration snippet.
location /files/ {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
This configuration enables directory listing for the /files/ path, showing file names and modification times.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx reads each file entry in the directory one by one.
- How many times: Once for every file or folder inside the directory.
As the number of files grows, nginx must read more entries to list them all.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads 10 file entries |
| 100 | Reads 100 file entries |
| 1000 | Reads 1000 file entries |
Pattern observation: The work grows directly with the number of files. Double the files, double the reading work.
Time Complexity: O(n)
This means the time to list files grows in a straight line with the number of files in the directory.
[X] Wrong: "The directory listing time stays the same no matter how many files there are."
[OK] Correct: nginx must read each file entry to show it, so more files mean more reading and more time.
Understanding how directory listing scales helps you reason about server performance and user experience when serving many files. This skill shows you can think about real-world system behavior.
"What if nginx cached the directory entries instead of reading them every time? How would the time complexity change?"
Practice
autoindex on; directive do in an nginx server block?Solution
Step 1: Understand the purpose of
Theautoindexautoindexdirective controls whether nginx shows a list of files in a directory when no index file is found.Step 2: Effect of
Setting it toautoindex on;onenables directory listing, allowing users to browse files via a web browser.Final Answer:
It enables directory listing so users can see files in a folder via browser. -> Option AQuick Check:
autoindex on = directory listing enabled [OK]
- Thinking autoindex disables access
- Confusing autoindex with compression
- Assuming autoindex redirects requests
Solution
Step 1: Recall nginx directive syntax
nginx directives use specific keywords; for enabling autoindex, the keyword ison.Step 2: Identify correct keyword
Onlyautoindex on;is valid syntax. Others likeenable,true, oryesare invalid.Final Answer:
autoindex on; -> Option BQuick Check:
Correct syntax = autoindex on; [OK]
- Using 'enable' instead of 'on'
- Using 'yes' or 'true' which are invalid
- Missing semicolon at end
location /files/ {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}What will the directory listing show regarding file sizes and timestamps?
Solution
Step 1: Understand
This setting shows file sizes in human-readable format (e.g., KB, MB) instead of bytes.autoindex_exact_size off;Step 2: Understand
This setting shows file timestamps in the server's local time zone, not UTC.autoindex_localtime on;Final Answer:
File sizes in human-readable format and timestamps in local time. -> Option AQuick Check:
autoindex_exact_size off + autoindex_localtime on = human sizes + local time [OK]
- Assuming sizes always show in bytes
- Thinking timestamps are always UTC
- Confusing off/on meaning for these directives
autoindex on; but directory listing still does not show. Which is the most likely cause?Solution
Step 1: Check nginx directory listing behavior
nginx shows directory listing only if no index file (like index.html) exists in the directory.Step 2: Identify why listing is not shown
If an index file is present, nginx serves it instead of showing directory listing, even ifautoindex on;is set.Final Answer:
The directory has an index.html file present. -> Option CQuick Check:
Index file present blocks directory listing [OK]
- Assuming misspelling causes no effect (it causes error)
- Thinking autoindex must be in http block only
- Ignoring presence of index files
/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?Solution
Step 1: Choose correct root and enable autoindex
Usingroot /var/www;withlocation /public/serves files from/var/www/public.autoindex on;enables directory listing.Step 2: Set human-readable sizes and local timestamps
autoindex_exact_size off;shows sizes in human-readable format, andautoindex_localtime on;shows timestamps in local time.Final Answer:
location /public/ { root /var/www; autoindex on; autoindex_exact_size off; autoindex_localtime on; } -> Option DQuick Check:
Correct root + autoindex on + human sizes + local time = location /public/ { root /var/www; autoindex on; autoindex_exact_size off; autoindex_localtime on; } [OK]
- Using alias incorrectly with root paths
- Turning autoindex off disables listing
- Setting exact_size on shows bytes, not human sizes
