Nginx: use try_files to serve file instead of directory

nginx

My server uses nginx, and my domain serves static html web pages, i.e.

http://server.com/download.html

For various reasons, I would like that users are able to fetch download.html through the following URLs:

http://server.com/download.html  (works)
http://server.com/download       (works)
http://server.com/download/      (works not)

I got the first two working, but the last one always returns /404.html.

Is try_files able to do what I want, or do I have to use the rewrite directive instead?

My nginx configuration:

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri.html $uri/ /404.html;
    # auth_basic "Restricted";
    # auth_basic_user_file /var/www/upscaledb/htpasswd;
}

Thanks,
Christoph

Best Answer

If you add a location to rewrite the URI without the trailing slash, then your existing location / and try_files directive can do the rest:

location ~ ./$ { 
    rewrite ^(.+)/$ $1 last; 
}