Enable or disable directory listing in Nginx

Enable or Disable Directory Listing in Nginx Configuration

Nginx provides flexible configuration options for serving content, including the ability to enable or disable directory listing. Directory listing displays a list of files within a directory when no index file (like index.html) is present. By default, Nginx does not allow directory listing; however, you can enable or disable it based on specific requirements.

Enabling or disabling directory listing in Nginx provides control over how directories are presented to users when they access a folder without an index file. By default, Nginx does not allow directory listing for security reasons, so it returns a “403 Forbidden” error if a directory lacks an index.html or index.php file. This configuration can be modified to suit the server’s needs.

In this guide, I’ll explain how to control directory listing in Nginx, using the autoindex directive.

Configuring Directory Listing in NGINX

To manage directory listings in NGINX, adjust the autoindex directive in the server block or location block of your NGINX configuration file.

Steps to Enable Directory Listing

Locate Your Configuration File: Open your NGINX configuration file, typically found in /etc/nginx/nginx.conf or in a separate server-specific file within /etc/nginx/sites-available/.

Define a Location Block: Within the server block, specify a location block for the directory you want to make accessible. For example:

Set the autoindex Directive: Use the autoindex on; directive to enable directory listing for the specified path. In this example, directory listing will be enabled for /files/, located at /var/www/html/files/.

Reload NGINX: Once you have updated and saved the configuration file, reload NGINX to apply the changes:

After reloading, navigating to http://example.com/files/ in a browser will display a list of files within the /files/ directory.

Disabling Directory Listing

If you want to ensure that directory listing is disabled, simply set autoindex to off:

Reload NGINX to apply the changes:

When autoindex is set to off, attempting to access a directory without an index file will result in a 403 Forbidden error.

Customizing Directory Listings

To customize the appearance and format of directory listings, you can use the following directives:

  • autoindex_exact_size: By default, NGINX shows the exact file sizes in bytes. To display sizes in a human-readable format, set autoindex_exact_size off;.
  • autoindex_localtime: NGINX displays file modification times in UTC by default. To show the time in the server’s local timezone, use autoindex_localtime on;.

After making these changes, reload NGINX:

Summary

The autoindex directive in NGINX provides flexibility for displaying directory listings. Whether you need to enable or disable directory listings, the configuration is simple and requires only minor adjustments. Fine-tuning autoindex options allows for enhanced control over how directory contents are presented to users.

For more details, refer to the official NGINX documentation.

Further reading

Leave a Reply

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