Score:0

Serve nginx's autoindex under different path

il flag

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.

us flag
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.
il flag
@TeroKilkanen `http://example.com/path/to/dir/autoindex.html` should server the auto index of `$webroot/path/to/dir`
Score:0
il flag

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.

Score:0
us flag

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
}
il flag
Well I was asking about having the auto index page on a different path than one ending in `/`. I know how to enable autoindex
us flag
Ah, how silly of me. I updated the answer.
il flag
Sadly that returns a 404 for every URI with `/path`. Changing the location to `location = /path/` fixes that part but `/path/autoindex.html` still returns a 404.
us flag
I added a `location` block that handles external requests to `/path`.
il flag
Still 404 for `/path/autoindex.html`
il flag
Even changing the `try_files $uri $uri/ =404;` to `try_files $uri $uri/ =403;` still returns a 404, telling me that it doesn't seem to hit the other location block.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.