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
Enable Directory Listing with Nginx Autoindex
📖 Scenario: You are setting up a simple web server using Nginx. You want visitors to see a list of files when they visit a folder without an index file.
🎯 Goal: Configure Nginx to show directory listings (autoindex) for a specific folder on your server.
📋 What You'll Learn
Create a server block configuration file for Nginx
Set the root directory to /var/www/html/files
Enable autoindex directive to show directory listing
Reload Nginx to apply the changes
Verify the directory listing is shown when accessing the folder
💡 Why This Matters
🌍 Real World
Web servers often need to show directory contents when no index file is present, such as for file sharing or downloads.
💼 Career
Knowing how to configure Nginx autoindex is useful for system administrators and DevOps engineers managing web servers.
Progress0 / 4 steps
1
Create Nginx server block configuration
Create a file called /etc/nginx/sites-available/files.conf with a server block that listens on port 80 and sets the root directory to /var/www/html/files.
Nginx
Hint
Use server { ... } block with listen 80; and root /var/www/html/files;.
2
Enable autoindex directive
Inside the server block in /etc/nginx/sites-available/files.conf, add the line autoindex on; to enable directory listing.
Nginx
Hint
Add autoindex on; inside the server block.
3
Create symbolic link to enable site
Create a symbolic link from /etc/nginx/sites-available/files.conf to /etc/nginx/sites-enabled/files.conf using the command ln -s /etc/nginx/sites-available/files.conf /etc/nginx/sites-enabled/files.conf.
Nginx
Hint
Use ln -s command to create the symbolic link.
4
Reload Nginx and verify directory listing
Reload Nginx with sudo systemctl reload nginx and then access http://localhost/ in your browser to see the directory listing of /var/www/html/files. Write a print statement that outputs Directory listing enabled to confirm.
Nginx
Hint
Use sudo systemctl reload nginx to reload and print("Directory listing enabled") to confirm.
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
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.
Step 2: Effect of autoindex on;
Setting it to on enables 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 A
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
Step 1: Recall nginx directive syntax
nginx directives use specific keywords; for enabling autoindex, the keyword is on.
Step 2: Identify correct keyword
Only autoindex on; is valid syntax. Others like enable, true, or yes are invalid.
Final Answer:
autoindex on; -> Option B
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:
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
Step 1: Understand autoindex_exact_size off;
This setting shows file sizes in human-readable format (e.g., KB, MB) instead of bytes.
Step 2: Understand autoindex_localtime on;
This setting shows file timestamps in the server's local time zone, not UTC.
Final Answer:
File sizes in human-readable format and timestamps in local time. -> Option A
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
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 if autoindex on; is set.
Final Answer:
The directory has an index.html file present. -> Option C
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?