baicai

白菜

一个勤奋的代码搬运工!

Enable directory indexing in nginx to display a list of files.

In nginx, if there is no index.html file in a specific directory, it will return a 404 Not Found error by default.

However, the Nginx autoindex module - ngx_http_autoindex_module provides a method to automatically generate a list, and enabling autoindex is very easy, just use autoindex on. The following configuration will return the directory structure when accessing a specific request.

Official reference: http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

server {
        listen   80;
        ... ...
        
        location /index_dir {
        autoindex on;
        }
}

In addition to simply turning autoindex on or off, you can also configure it in other ways, including:

autoindex_exact_size; Display the exact file size or the closest KB, MB, or GB. The default is on, which displays the exact size of the file in bytes. When set to off, it displays the approximate size of the file in kB, MB, or GB.
autoindex_format; This directive specifies the format in which the Nginx index list should be output. There are four options for this directive: html/xml/json/jsonp.
autoindex_localtime; The displayed file time is in GMT. Note: When set to on, the displayed file time is the server time of the file.

Using these configurations, the configuration content will be similar to the following:

location /index_dir/ {
root   /data/index_dir/;  
autoindex on;
autoindex_exact_size off;
autoindex_format html;
autoindex_localtime on;
}

If there are Chinese directories, there may be garbled characters, so you need to add the following line below:

charset utf-8;

or

charset utf-8,gbk;
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.