0
0
Nginxdevops~5 mins

Directory listing (autoindex) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to see all files inside a folder on your web server when no specific file is requested. Directory listing, also called autoindex, lets your web server show a list of files in a folder automatically.
When you want to share files in a folder over the web without creating a webpage for each file.
When you need to quickly check which files are inside a web directory from a browser.
When you want to enable browsing of logs or reports stored in a server folder.
When you are developing and want to see all static files available in a directory.
When you want to provide downloads of multiple files without a custom interface.
Config File - nginx.conf
nginx.conf
server {
    listen 8080;
    server_name localhost;

    location /files/ {
        root /var/www/html;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
}

This configuration creates a server listening on port 8080.

The location /files/ block points to the folder /var/www/html/files/ on the server.

autoindex on; enables directory listing for this location.

autoindex_exact_size off; shows file sizes in a human-friendly format (KB, MB).

autoindex_localtime on; shows file times in local time instead of GMT.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Request the directory URL to see the autoindex directory listing in the terminal.
Terminal
curl http://localhost:8080/files/
Expected OutputExpected
<html> <head><title>Index of /files/</title></head> <body bgcolor="white"> <h1>Index of /files/</h1><hr><pre><a href="file1.txt">file1.txt</a> 01-Jan-2024 10:00 1.2K <a href="image.png">image.png</a> 02-Jan-2024 11:30 45K </pre><hr></body> </html>
Key Concept

If you remember nothing else from this pattern, remember: enabling autoindex in nginx lets users see a list of files in a folder when no index file is present.

Common Mistakes
Forgetting to reload nginx after changing the config file.
The new settings won't take effect until nginx reloads the configuration.
Always run 'sudo systemctl reload nginx' after editing nginx.conf.
Setting the wrong root path in the location block.
Nginx will look in the wrong folder and show a 404 error or empty listing.
Make sure the root path plus the location path matches the actual folder on disk.
Not enabling autoindex directive or setting it to off.
Nginx will not show directory listings and may return a 403 forbidden error.
Set 'autoindex on;' inside the location block to enable directory listing.
Summary
Edit nginx.conf to add a location block with 'autoindex on;' to enable directory listing.
Test the configuration syntax with 'nginx -t' before reloading.
Reload nginx to apply changes and then access the directory URL to see the file list.