Nginx Configuration – Serve Nginx’s Autoindex Under Different Path

configurationdirectoryindexnginx

I've run into the issue that I would like to enable nginx's autoindex for some directories but those also having their own index files.
So I was wondering if there was a way to make nginx serve it's autoindex page on a different path. Something like /path/to/dir/autoindex.html

I tried the following:

    location ~* ^/path/to/dir/autoindex.html$ {
        autoindex on;
        autoindex_format html; 

        try_files /path/to/dir/ =404;
    }

But that strangely just redirects me to /path/to/dir/ and shows me my default index page.

Additionally I would like to keep this for folders that don't have an index page, just so the path for the autoindex is always consitent.

Best Answer

nginx internal rewrite might be applicable here:

location /path/autoindex.html {
    rewrite ^ /path/ last;
}

location /path {
    internal; # This location is only used for internal redirects

    autoindex on;

    try_files $uri $uri/ =404;
}

location ~ ^/path {
    ... configure what you want to show with the path
}
Related Topic