2

http://localhost:8001/whatever loads /var/www/nginx.html but http://localhost:8001 loads /var/www/index.html

According to the grep result, there is no uncommented index.html mentioned whatsoever.

What I expect is to load nginx.html in the second case too.

The only thing I found is when I remove $uri/ from try_files it works as expected.

But basically where is index.html configuration? is it nginx's default behavior?

It's good to say that index.php is not loaded. I've checked many times and sure about the file.

grep result

default config file

sinaza
  • 123
  • 1
    Instead of screenshots, consider copying and pasting your config into your posts instead; text loads faster than screenshots. – Thomas Ward Nov 23 '17 at 18:27

1 Answers1

2

When otherwise unspecified, the index directive has an implied default of index.html. Which is present in your document root. (See the documentation below that I included detailing defaults and such)

If you want to change what it serves as its index page, you need to define a new index directive, such that you add this to your / location block, or apply it globally by just putting it outside the location blocks but still within your server block:

index nginx.html;

This tells nginx to instead look for a different index file.

The problem you're seeing is because the / location block only applies if you have the / specified, and because you don't allow autoindex and because /whatever is your URI and that doesn't exist either as a file or a directory, it falls back to your third option, the nginx.html file. I know this is counter-intuitive, as an empty URI or a / URI typically means "Process this per the location / { } block, but it's more implied in the world that this takes you to the home or index page of a given website, and that is why the index directive exists.

You need to tell the system what to treat as an index file for any configuration. You can't rely solely on try_files in your / location block, you have to tell it what your index file should be. I always define this even if the defaults work fine for me.

Note that if you don't define a URI, try_files falls back to the index directive, so a request to http://foobarbaz.foo:8510 will reach the nginx server block listening on port 8510 for the server name / $HOST request record of foobarbaz.foo, with an empty URI, and is technically going to have nginx, internally, try and serve the default index.html index page because of your configuration not specifying a different index to serve for any type of request.


From the related nginx documentation of the index directive:

Syntax: indexfile ...;
Default: index index.html;
Context: http, server, location

Defines files that will be used as an index. The file name can contain variables. Files are checked in the specified order. The last element of the list can be a file with an absolute path. Example:

index index.$geo.html index.0.html /index.html;

It should be noted that using an index file causes an internal redirect, and the request can be processed in a different location. For example, with the following configuration:

location = / {
    index index.html;
}

location / {
    ...
}

... a “/” request will actually be processed in the second location as “/index.html”.

Thomas Ward
  • 74,764