Enabling the Nginx Directory Index Listing

Directory listing is a feature that when enabled the web servers list the content of a directory when there is no index file (e.g. index.php or index.html) present.

Designed to be navigated by clicking directory links, directory listings typically have a title that describes the current directory, a list of files and directories that can be clicked.

Enabling directory listing in a folder in nginx is simple enough with just an  “autoindex on;” directive inside the location directive.

location /Somedirectoryname/ {
    autoindex on; }

Nginx autoindex directives

Besides simply using autoindex on or off, there are also three other directives you can configure with this module. These include the following:

  • autoindex_exact_size; – This directive specifies whether Nginx should display the exact file sizes of the output in the directory index or simply round to the nearest KB, MB, or GB. This directive has two options: on or off.
  • autoindex_format; – This directive specifies what format the Nginx index listing should be outputted to. This directive has four options: htmlxmljson, or jsonp.
  • autoindex_localtime; – This directive specifies whether the times for the directory listing should be outputted to local time or UTC. This directive has two options: on or off.

Below is an example of a location directive using all four autoindex :-

location /somedirectory/ {
    autoindex on;
    autoindex_exact_size off;
    autoindex_format html;
    autoindex_localtime on; }

Enabling sitewide directory listing

You can also enable sitewide directory listing by putting it in the server block or even enable directory access for all sites by putting it in the http block.

server {
        listen   80;
        server_name  test.com www.test.com;
        access_log  /var/...........................;
        root   /var/www/html;
        autoindex on;
        }

Leave a Reply

Your email address will not be published. Required fields are marked *