How to configure Nginx to serve a different index page for directory requests?

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:

  1. Locate the Nginx configuration file. In most cases, it is located at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.

  2. Open the configuration file in a text editor.

  3. 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; } ... }
  4. Save the configuration file and exit the text editor.

  5. 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.

  6. 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.