Conflict: index and try_files

In the following simple configuration

server {
server_name testsite.static;
root /some/path/;
index blog.html;
try_files $uri $uri.html =404;
}

the try_files directive seems to interfere with the index directive: I
always get 404 for http://testsite.static/. If I remove the fallback
part of try_files, the same request results in an error:

*990 rewrite or internal redirection cycle while internal redirect to
"/.html.html.html…

The file blog.html is present in /some/path/, so I really do not
understand why the file is not found/served for the above URL.

Posted at Nginx Forum:

On Fri, Nov 18, 2011 at 09:39:05AM -0500, janedenone wrote:

always get 404 for http://testsite.static/. If I remove the fallback
part of try_files, the same request results in an error:

*990 rewrite or internal redirection cycle while internal redirect to
"/.html.html.html…

The file blog.html is present in /some/path/, so I really do not
understand why the file is not found/served for the above URL.

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


Igor S.

Excellent, this works.

Thanks a lot.

Posted at Nginx Forum:

Hello!

On Fri, Nov 18, 2011 at 09:39:05AM -0500, janedenone wrote:

always get 404 for http://testsite.static/.
If you want try_files to test directories as well (and to allow
index directive to work), you have to explicitly specify “/” at
the end of name. I.e. use something like this:

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

If I remove the fallback
part of try_files, the same request results in an error:

If you remove “=404” from the above try_files, it will become

try_files $uri $uri.html;

i.e. if no file $uri found, it will redirect to $uri.html (/.html)
in your case. In it’s turn it won’t be found and will again
redirect to $uri.html (/.html.html now). The

*990 rewrite or internal redirection cycle while internal redirect to
"/.html.html.html…

is expected.

The file blog.html is present in /some/path/, so I really do not
understand why the file is not found/served for the above URL.

That’s because it tests for files, and rejects directories by
default. Test for directories must by explicitly requested.

See here for more information:

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

Maxim D.

Thanks again for the helpful hint. I noticed that it seems to be
possible to remove the $uri part –

try_files $uri/ $uri.html =404;

– and URLs like /image.jpg, /style.css etc are still found. I would
expect that these requests return 404, because nginx tries image.jpg/
and then image.jpg.html, but not the actual $uri. Am I missing
something?

Posted at Nginx Forum: