2

I have the following server configuration:

server {
    listen 80;
    server_name _;

    root /var/www/;

    location /calendars/ {
        autoindex on;
        try_files $uri.ics $uri =404;
    }
}

If would expect to get the autoindex page on visiting http://example.com/calendars/, however I get an 404 File not found error instead.

I want the server do something like this pseudo-code:

if($uri is directory) {
    if(one of index pages exists in directory) {
        show index page;
    } else {
        show autoindex page;
    }
} else {
    if($uri.ics exists) {
        show $uri.ics;
    } else if($uri exists) {
        show $uri;
    } else {
        show 404 page;
    }
}

1 Answer 1

7

You also need a check for the directory in try_files if you want the directory index loaded or autogenerated.

try_files $uri.ics $uri $uri/ =404;

Per the documentation for the try_files directive:

It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. $uri/

In other words, $uri means "try a file at the given path", whereas $uri/ means "try a directory at the given path", and the latter is what causes auto-indexing of that directory to kick in.

4
  • This works, but I don't understand why. If I visit /calendar/ wouldn't $uri == "/calendar/" and $uri/ == "/calendar//"?
    – Tyilo
    Mar 27, 2013 at 11:40
  • 1
    Did you click the link? It explains: It doesn't actually append a /, but instead loads the directory index if available. Mar 27, 2013 at 11:49
  • I hadn't noticed the link as I'm colorblind.
    – Tyilo
    Mar 27, 2013 at 11:54
  • Blame the developers. They're hard to see for the rest of us, too. :) Mar 27, 2013 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.