Reputation: 11920
I have a simple nginx configuration file -
server {
listen 80 default_server;
root /var/www/example.com;
#
# Routing
#
location / { index index.html; }
location /foo { index foo.html }
#
# Logging
#
access_log /var/log/nginx/{{ www_domain }}.log;
error_log /var/log/nginx/{{ www_domain }}-error.log error;
server_name example.com;
charset utf-8;
}
As you can see, there's only 2 routes - the /
and /foo
paths.
When I go to www.example.com/
all works correctly. I can see the index.html
page being served.
When I go to www.example.com/foo
, I get a 404 error when I should be getting the foo.html
page.
Looking at the logs, I see this error:
2018/08/13 21:51:42 [error] 14594#14594: *6 open() "/var/www/example.com/foo" failed (2: No such file or directory), client: XX.XX.XX.XX, server: example.com, request: "GET /foo HTTP/1.1", host: "example.com"
The error implies that it's looking for a file named /var/www/example.com/foo
and not /var/www/example.com/foo.html
like I would expect.
Why does this happen in general, and specifically why does it not happen on my root path /
?
Thanks!
Edit: It does work if I visit www.example.com/foo.html
directly
Upvotes: 1
Views: 919
Reputation: 49812
The index
directive will append the filename, when you give the URI of a directory.
So /
points to the root directory (/var/www/example.com/
), and the index index.html;
statement causes nginx
to return the file /var/www/example.com/index.html
.
The /foo
URI does not point to a directory. If the directory /var/www/example.com/foo/
) did in fact exist, the index foo.html;
statement would cause nginx
to return the file /var/www/example.com/foo/foo.html
. Which is not /var/www/example.com/foo.html
.
What you are attempting to achieve is some kind of extension-less scheme, which is nothing to do with the index
directive.
See this document for details of the index
directive.
There are many solutions that will work, for example, using try_files
instead of index
:
location /foo { try_files /foo.html =404; }
See this document for details.
Upvotes: 1