How to enable directory listing in Nginx

Spread the love

Enabling the Nginx Directory Index Listing

Nginx is a popular web server that can also be used as a reverse proxy, caching, load balancing, and for other purposes. In this post, I’ll show you how to enable directory listing. When an index file (such as index.html or index.php) is not found, NGINX will automatically generate and display a list of files in the directory. This occurs when a user navigates to a directory on a web server and there is no default file for that directory.

For example, if you do not have an index.html file in your server’s docs directory, when someone visits http://example.com/docs/, NGINX will generate a list of files and subdirectories within the docs’ directory and display it in the web browser. This feature is useful for file sharing or providing a directory listing of documents or media files. It can, however, pose a security risk if sensitive information is exposed.

If you are using the Nginx web server and want to display folders in Nginx by web, you can use the autoindex directive in your NGINX configuration to enable or disable directory listing.” in your conf file. By default, we cannot open a directory from the Nginx web.

Enable directory listing

Open your nginx conf file and Update "autoindex on;" as describe below.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
	      root /var/www/html;
	      index index.html index.htm index.nginx-debian.html;
        autoindex on; #for enable directory listing 
        server_name _;
        location / {
	      try_files $uri $uri/ =404;
        }
}

How to disable Directory Indexing in Nginx

See also  Linux Network Bandwidth check Script.

Here is the simple step to disable directory listing in Nginx. Go to the Nginx configuration file and turn off the indexing.

autoindex off;

This adjustment ensures that users won’t be able to view directory listings, reinforcing the security of your web server.

Conclusion:

In the realm of Nginx, the ability to control directory listing adds an extra layer of customization and security. Whether you choose to enable or disable this feature, understanding how to manipulate the ‘autoindex’ directive empowers you to tailor your Nginx configuration to meet your specific requirements.

Stay tuned for more insights into optimizing and mastering Nginx on our blog.

Here is the video link of the tutorial.

1 thought on “How to enable directory listing in Nginx”

Leave a Comment