How to enable directory listing in Nginx?

Setting up nginx to list files and folders in a directory.

Posted by Umut Canbolat on June 10, 2018 · 2 mins read

I have just migrated my website from Apache to Nginx. The reasons for this can be another topic of a new blog post. But the main reason for me was to learn something new.

I use this website not only for blog posting but also for sharing files. Sometimes I need to make some files or folders online. On Apache, the folders which are located under /var/www/html can be listed with an HTTP GET request on the browser by default. But Nginx web server needs a simple configuration.

Configuring Nginx

When the web server receives an HTTP GET request, it first looks for some index files like index.php in the requested directory. If it can’t find any index file, then It shows “HTTP 403 forbidden” error. To make the web server list the files and folders under the directory requested, we need to add autoindex on; in /etc/nginx/sites-available/default file.

To make directory listing enabled for all site, we can put it like following:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        root /var/www/html;

        index index.php index.html index.htm;

        server_name domain.com www.domain.com;

        location / {
                autoindex on;
        }
}

To make only specific folders available for listing, go for it like this:

location /testDirectory/ {
    autoindex on;
}

Restart Nginx to apply your changes. service nginx restart

Result

When I make GET request for the directory I’ve configured, it lists files and folders like this:

nginx-directory-listing

References