要在Nginx中启用目录浏览功能,您需要修改Nginx配置文件。以下是详细步骤:
打开Nginx配置文件(通常是/etc/nginx/nginx.conf
或/etc/nginx/sites-available/your-site
)
在需要启用目录浏览的location
块中添加以下指令:
location /path/to/directory {
autoindex on; # 启用目录列表
autoindex_exact_size off; # 显示文件大小以易读格式(KB,MB,GB)
autoindex_localtime on; # 使用本地时间显示文件修改时间
}
server {
listen 80;
server_name example.com;
location /downloads {
alias /var/www/downloads;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
}
autoindex_format
:指定目录列表格式(html/xml/json/jsonp)autoindex_exact_size
:on显示精确字节数,off显示易读格式autoindex_localtime
:on使用本地时间,off使用UTC时间启用目录浏览可能会暴露敏感信息,建议:
allow
/deny
指令限制访问IP.htaccess
或Nginx的auth_basic
添加基础认证sudo nginx -t
sudo systemctl reload nginx
完成以上步骤后,访问配置的目录路径应该就能看到文件列表了。