index
The 'index' directive defines the default file to be served when a directory is requested.
Description
The 'index' directive in NGINX specifies the default file that should be returned when a user requests a directory. This directive is particularly useful in cases where a directory does not contain an index file specified by the user, or when a user directly requests a directory path. The server will look for the specified index files in the order they are listed until one is found, or until all options are exhausted. This allows for customization of default behavior based on application needs.
Multiple index files can be specified by separating them with spaces, and NGINX will check for the presence of each file in the given order. If none of the specified files are found, NGINX will return a 403 Forbidden or 404 Not Found error, depending on configuration settings. This flexibility makes the 'index' directive a key part of NGINX's ability to serve dynamic web applications and static content seamlessly.
Config Example
location / {
index index.php index.html index.htm;
}Make sure the files specified actually exist, otherwise an error may occur when accessing the directory.
Remember that if no index file is found, a directory listing may be shown if enabled; ensure this is desired behavior.
When using multiple index files, they should not contain spaces unless quoted, as spaces are the delimiter.