using nginx try_files and autoindex together

Tyilo asked:

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;
    }
}

My answer:


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;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.