Is it possible to serve the same root from autoindex both as JSON and HTML with nginx?

With nginx I'd like to be able to serve the same location both as JSON and as HTML

So the path /files/ could serve /srv/www/files/ with autoindex as HTML and /api/ would serve the listing of /srv/www/files/ as JSON.

Is that possible?

I've been trying with rewrite and different locations but failed.


Solution 1:

Use root or alias within the location block to specify the path to the files. Use alias when the path cannot be calculated using simple concatenation of the document root with the URI. See this document for details.

For example:

root /srv/www;

location /files/ { 
    autoindex on; 
}

location /api/ { 
    autoindex on; 
    autoindex_format json; 
    alias /srv/www/files/;
}