Nginx · December 19, 2023

Nginx Tip - Configure autoindex for directory listing

Nginx Tip - Configure autoindex for directory listing

Nginx is a popular web server known for its high performance, scalability, and flexibility. It is widely used to serve static content, reverse proxy, and load balance web applications. One of the key features of Nginx is its ability to configure autoindex for directory listing. In this article, we will explore how to enable and customize autoindex in Nginx.

Autoindex is a module in Nginx that generates directory listings for directories that do not have an index file. By default, Nginx disables autoindex, which means that when a user accesses a directory without an index file, they will see a "403 Forbidden" error. However, enabling autoindex allows Nginx to generate a directory listing, making it easier for users to navigate through the files and folders.

To enable autoindex in Nginx, you need to modify the Nginx configuration file. The configuration file is usually located in the /etc/nginx directory and is named nginx.conf or default.conf. Open the configuration file using a text editor and locate the server block that corresponds to your website.

Within the server block, add the following line to enable autoindex:

```
location / {
autoindex on;
}
```

Save the configuration file and restart Nginx for the changes to take effect. Now, when a user accesses a directory without an index file, they will see a directory listing instead of a "403 Forbidden" error.

While the default autoindex configuration is functional, you can customize it to enhance the user experience. Nginx provides several directives that allow you to control the appearance and behavior of the directory listing. Here are some commonly used directives:

- `autoindex_exact_size`: When set to on, displays the exact file size instead of using human-readable units.
- `autoindex_localtime`: When set to on, displays the file modification time in the local time zone.
- `autoindex_format`: Specifies the format of the directory listing. The default format is `html`, but you can also use `xml` or `json`.
- `autoindex_ignore`: Specifies files or directories to be ignored in the directory listing. You can use wildcards to match multiple files or directories.

To customize the autoindex configuration, add the desired directives within the location block:

```
location / {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format html;
autoindex_ignore *.txt;
}
```

Save the configuration file and restart Nginx for the changes to take effect. Now, the directory listing will reflect your customizations.

In conclusion, configuring autoindex in Nginx allows you to generate directory listings for directories without index files. By enabling and customizing autoindex, you can enhance the user experience and make it easier for users to navigate through your website's files and folders.

If you are looking for a reliable VPS hosting provider in the Hong Kong, consider Server.HK. With top-notch VPS solutions and excellent customer support, Server.HK is the ideal choice for your hosting needs. Visit their website at https://server.hk to learn more about their services.