自 1.7.9 版(2014 年 12 月 23 日发布)起,NGINX 中包含的ngx_http_autoindex_module 模块(为以斜杠字符结尾的请求生成目录列表:/) 添加一个autoindex_format directive,它设置目录列表的格式:
语法: autoindex_formathtml | xml | json | jsonp;
默认值: autoindex_format html;
上下文: http, server, location
示例 启用此功能的配置(对于 JSON):
location / {
autoindex on;
autoindex_format json;
}
当使用 JSONP 格式时,回调函数的名称使用 callback 请求参数设置。 如果该参数缺失或值为空,则返回 JSON 格式。
JSON(P) 响应数据结构(方案)定义为:
callback( // when format directive jsonp AND callback argument in request
[ // a single one-dimensional Array (wrap) containing
{ // Objects representing directory entries, structured as:
"name" :"*" //String
, "type" :"directory"|"file"|"other" //String, ONLY 1 OF THESE 3
, "mtime":"Ddd, DD Mmm YYYY hh:mm:ss GMT" //String, RFC1123-date of HTTP-date defined by RFC2616
, "size" :* //Integer, ONLY PRESENT IFF "type":"file" !!!!!
} /*
, { // and repeating the above Object structure for each directory entry
} */
]
); // end callbackFunction-call when jsonp
"name"、"mtime"、"type":"directory" 和 "type":"file" 应该是不言自明的。
重要要注意 "size" 字段仅存在于 IFF @ 987654343@,否则完全省略(参见下面的示例)!
"type":"other" 值得进一步解释:
假设您做了一些您在生产中永远不会做的事情,并在 linux 系统的配置中设置 'root /;',然后索引 /dev/,那么您将获得如下目录条目:
[
{ "name":"floppy", "type":"other", "mtime":"Mon, 23 Oct 2017 15:16:56 GMT" },
{ "name":"loop0", "type":"other", "mtime":"Mon, 23 Oct 2017 15:16:56 GMT" }
// and so forth
]
"type":"other"可能还有其他例子,如果你知道一些,请发表评论!
特殊点当前/点父(.,..)和点文件/点文件夹(.hidden)在响应中被过滤(不存在)!
有趣的是:特殊的 dot-current/dot-parent (., ..) 可以由您的客户端代码添加,就像 ngx_http_autoindex_module 将它们硬编码到默认的 html 格式响应中一样。
以下是原始“格式”中的两个“原始”响应示例。
注意:响应行结尾被硬编码为CRLF。
JSON(和没有回调的 JSONP):
[
{ "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
{ "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
{ "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 }
]
JSONP(带有 callback=foo):
注意:前导块注释是 JSONP 响应的一部分。
/* callback */
foo([
{ "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
{ "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
{ "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 }
]);
一个非常简单的 sn-p 消耗一个 JSONP 回调并构建一个 HTML 表,旨在明确地阐明格式:
<script>
function foo(d){
var i= 0
, L= d.length
, r= '<table border="1" cellpadding="4px" style="border-collapse:collapse"><thead style="border-bottom:4px solid black">\n'
+ '<tr><th style="width:80%">Name</th><th>Type</th><th>Last Modified</th><th>Size</th></tr>\n'
+ '</thead><tbody>\n'
;
for(;i<L;++i)
r+= '<tr><td>' + d[i].name
+ '</td><td>' + d[i].type
+ '</td><td style="white-space: nowrap">' + d[i].mtime
+ '</td><td>' + (d[i].type==='file' ? d[i].size : '-')
+ '</td></tr>\n';
r+='</tbody></table>';
document.body.innerHTML=r;
}
</script>
<script> // JSONP Use your own preferred JSON(P) fetchmethod
/* callback */
foo([
{ "name":"subdir", "type":"directory", "mtime":"Tue, 24 Oct 2017 16:16:16 GMT" },
{ "name":"image.jpg", "type":"file", "mtime":"Tue, 24 Oct 2017 16:16:39 GMT", "size":5 },
{ "name":"test.html", "type":"file", "mtime":"Tue, 24 Oct 2017 16:09:18 GMT", "size":5 },
{ "name":"????", "type":"other", "mtime":"Tue, 24 Oct 2017 16:17:52 GMT" }
]);
</script>
一些额外的灵感可能来自:
来源: