What if your server could list all your files for you, instantly and perfectly every time?
Why Directory listing (autoindex) in Nginx? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
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.
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.
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.
Create index.html with links to each file
Update index.html every time files changelocation /files/ {
autoindex on;
}You can instantly share and browse files on your server without extra work or mistakes.
A photographer shares a folder of photos with clients by enabling autoindex, so clients can see and download all pictures easily.
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
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
