Bobcares

Try our smart search search

Learn how to use the nginx autoindex_format. Our Live Support Team is always here to guide you.

Understanding nginx autoindex_format

Nginx autoindex_format If you’re using Nginx to serve files and directories, chances are you’ve come across the need to display a directory listing when no index.html file is present. That’s where the powerful ngx_http_autoindex_module comes in, and more specifically, the nginx autoindex_format directive.

In this blog, we’ll cut through the noise and get straight to what matters, how to use nginx autoindex_format, what it does, and how it fits into your Nginx configuration. Whether you’re self-hosting a file repository or running a lightweight server setup, this guide will help you present your directory listings in the format you want.

What Is Nginx Autoindex?

The autoindex directive in Nginx enables automatic directory listings. When a URL ends with a / and no index.html is found, Nginx can generate a file list for that directory. This is handled by the ngx_http_autoindex_module, and it’s incredibly useful when you want quick access to files without building a front-end or manually creating an index page.

Here’s the basic configuration:

location /directory_name/ {
autoindex on;
}Copy Code

The location directive tells Nginx which URI path to handle. Setting autoindex on; enables the directory listing if no index file exists.

After making changes to the configuration file, always remember to restart the Nginx server to apply them. Without a restart, the settings won’t take effect and may even lead to issues like nginx error 99 cannot assign requested address.

Diving Deeper into Nginx Autoindex Directives

When you enable autoindex, Nginx doesn’t just give you a raw listing. You can customize how that listing appears using three important directives:

1. autoindex_exact_size

Controls whether file sizes are displayed as exact bytes or rounded to kilobytes, megabytes, etc.

autoindex_exact_size off;Copy Code

Setting this to off displays sizes in a human-readable format. Use on to show exact byte counts.

2. autoindex_localtime

By default, Nginx shows timestamps in UTC. To show them in local server time, use:

autoindex_localtime on;Copy Code

This is especially useful if you’re managing local servers or need consistency with your system logs.

3. nginx autoindex_format

Here’s where things get flexible. The nginx autoindex_format directive defines the output format of the directory listing.

autoindex_format html;Copy Code

You have four options:

  • html: Default and most browser-friendly
  • xml: Useful for machine parsing
  • json: Great for API-style responses
  • jsonp: Allows for cross-domain JavaScript access

Choose the format that matches your use case. For most users, html works just fine. However, if you’re integrating this listing into an app or external tool, setting nginx autoindex_format to json or xml might be more appropriate.

Complete Configuration Example

Here’s a full e[If needed, Our team is available 24/7 for additional assistance.]xample using all three directives:

location /nginx_autoindex/ {
autoindex on;
autoindex_exact_size off;
autoindex_format html;
autoindex_localtime on;
}Copy Code

Once applied and the server is restarted, visiting /nginx_autoindex/ will display a clean, formatted directory listing with file sizes and timestamps tailored to your preferences.

[If needed, Our team is available 24/7 for additional assistance.]

Conclusion

The nginx autoindex_format directive adds versatility to the already handy autoindex feature. Whether you’re building a file-sharing hub, exposing logs, or serving up static content, configuring this module properly can improve usability and clarity.

Use nginx autoindex_format wisely depending on your needs, html for user-facing lists, json or xml for APIs. Keep in mind that this functionality only kicks in when there’s no index file in the directory. Always test and restart your server after any changes.

Need quick file access without overbuilding? Let nginx autoindex_format do the work for you.