0

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.

BrainStone
  • 103
  • 1
  • 1
  • 9
  • Please provide a concrete example of a request and what exactly it should serve. It is difficult to see which URL you want to request and what filesystem path autoindex you want to get returned. – Tero Kilkanen Jan 27 '22 at 18:47
  • @TeroKilkanen `http://example.com/path/to/dir/autoindex.html` should server the auto index of `$webroot/path/to/dir` – BrainStone Jan 27 '22 at 18:49

2 Answers2

0

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
}
Tero Kilkanen
  • 34,499
  • 3
  • 38
  • 58
0

I found a pretty decent solution that just cleverly uses redirects and their order:

server {
    # listen directives etc...

    root /path/to/web/root/dir;

    # Autoindex only shows when nginx can't file its own index files
    index xxx;

    rewrite ^(?<path>.*)/autoindex\.html$ $path/           last;
    rewrite ^(?<path>.*)/$                $path/index.html last;

    autoindex on;

    # rest of server configuration...
}

Only downsite this has is that you can't really use the multiple different index files the index directive normally supports. try_files can also mess this up, as you need to make sure that for the <path>/ URI nginx can't find any files so it shows the autoindex.

Wouldn't recommend this on anything but a server or location that servers only static files.

BrainStone
  • 103
  • 1
  • 1
  • 9