To configure Nginx to serve a different index page for directory requests, you can use the index
directive and specify a new index file for the desired directories. Follow these steps:
Locate the Nginx configuration file. In most cases, it is located at /etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
.
Open the configuration file in a text editor.
Inside the server
block, add the index
directive with the desired index file name. For example, if you want to use directory.html
as the index file for directories:
server {
...
index index.html;
location /directory/ {
index directory.html;
}
...
}
Save the configuration file and exit the text editor.
Check the Nginx configuration for syntax errors using the command:
sudo nginx -t
If there are no errors, you can proceed to apply the configuration changes. Otherwise, review and fix the errors in the configuration file.
Restart Nginx to apply the changes:
sudo service nginx restart
Now, any directory requests to /directory/
will serve the directory.html
file as the index page, while other directories will continue to use the default index.html
file.