[英]Nginx list static files/directories as XML/Json
我安裝了nginx,旨在提供照片和視頻等靜態文件,能夠以編程方式列出所提供的文件。
問題是此列表是在HTML頁面中完成的,對於我的應用程序,我需要能夠處理此列表(類似於json或xml)。
無論如何我能用nginx做到這一點嗎? (或者還有其他解決方案可以解決我的問題)
提前致謝。
從版本1.7.9開始,您可以將autoindex_format
設置為json
或xml
,請參閱此處的文檔 。
location / {
...
autoindex on;
autoindex_format json;
}
自 1.7.1 版(2014-12-23發布)以來 ,NGINX中包含的ngx_http_autoindex_module模塊(生成以斜杠字符結尾的請求的目錄列表: /
)添加了autoindex_format
指令 ,用於設置目錄列表的格式 :
語法:
autoindex_format
html | xml | json | jsonp;
html | 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 "type":"file"
,否則它被完全省略(見下文實施例)!!
"type":"other"
值得進一步解釋:
假設你做了一些你從未在生產中做過的事情並設置'root /;'
在linux系統上的配置中,然后是index /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"
例子"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(帶回調= 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 }
]);
一個痛苦的簡單片段,它使用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>
這意味着從這里開始,您可以對客戶端的結果進行過濾和排序。 您還可以根據文件類型/擴展名等鏈接圖標。
祝你好運,祝你好運!祝你好運!
一些額外的靈感可能會在:
資料來源:
通過向服務器添加php腳本來列出文件來解決問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.