Nginx list static files/directories as XML/Json Nginx list static files/directories as XML/Json json json

Nginx list static files/directories as XML/Json


Since version 1.7.9 you can use autoindex_format set to json or xml, refer to the docs here.

location / {    ...    autoindex on;    autoindex_format json;}


The ngx_http_autoindex_module Module (which produces a directory listing for requests ending with the slash character: /) included in NGINX since version 1.7.9 (released 2014-12-23) added an autoindex_format directive that sets the format of a directory listing:

Syntax: autoindex_formathtml | xml | json | jsonp;
Default: autoindex_format html;
Context: http, server, location

Example config to enable this feature (for JSON):

location / {    autoindex on;    autoindex_format json;}

When the JSONP format is used then the name of a callback function is set with the callback request argument. If that argument is missing or has an empty value, then the JSON format is returned.

The JSON(P) response data structure (scheme) is defined as :

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

The "name", "mtime", "type":"directory" and "type":"file" should be pretty self-explanatory.
Important to note that the "size" field is ONLY present IFF "type":"file", otherwise it is omitted entirely (see examples below) !!

"type":"other" deserves some further explanation:
Suppose you did something you'd never do in production and set 'root /;' in the config on a linux system and subsequently index /dev/ then you'd get directory-entries like:

[{ "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]

There could be other examples for "type":"other", if you know some, please leave a comment!

Special dot-current/dot-parent (., ..) and Dot-files/dot-folders (.hidden) are filtered (not present) in the response!
Interesting: special dot-current/dot-parent (., ..) can be added by your clientside code, just as the ngx_http_autoindex_module has hardcoded them into the default html-format response.


Here are two 'raw' response examples in their original 'formatting'.
Note: response line-endings are hardcoded as CRLF.

JSON (and JSONP without callback):

[{ "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 (with callback=foo):
note: the leading block-comment is part of the JSONP response.

/* 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 }]);



A painfully simple snippet consuming a JSONP callback and building a HTML table intended to unambiguously clarify the format: