0
0
Nginxdevops~5 mins

Index directive in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a user visits a website folder without specifying a file, the server needs to know which file to show first. The index directive tells the server which file to look for and display automatically. This makes browsing websites smoother and friendlier.
When you want the server to automatically show the homepage file like index.html when someone visits your website root.
When you have multiple possible homepage files and want to set the order the server tries them.
When you want to change the default homepage file name to something custom like home.html.
When you want to prevent directory listing by ensuring a file is shown instead of a folder content.
When you want to serve a default file for a specific website location or folder.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

    root /var/www/html;

    index index.html index.htm home.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

The server block defines a website configuration listening on port 80 for example.com.

The root sets the folder where website files are stored.

The index directive lists files the server tries in order when a folder is requested.

The location / block handles requests to the root URL, trying to serve files or returning 404 if not found.

Commands
This command tests the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command requests the root URL from the local server to check which index file is served automatically.
Terminal
curl http://localhost/
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to example.com!</title> </head> <body> <h1>Hello from index.html</h1> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: the index directive tells nginx which file to serve first when a folder URL is requested.

Common Mistakes
Not reloading nginx after changing the index directive.
Nginx keeps using the old configuration until reloaded, so changes won't take effect.
Always run 'sudo nginx -t' to test and then 'sudo systemctl reload nginx' to apply changes.
Listing files in the wrong order in the index directive.
Nginx serves the first file it finds in the list, so order matters for priority.
Put the most preferred index file first in the list, e.g., 'index.html index.htm home.html'.
Forgetting to set the root directive correctly.
If root is wrong, nginx won't find the index files and may return 404 errors.
Ensure the root path points to the folder where your website files are stored.
Summary
The index directive sets which files nginx tries to serve automatically when a folder URL is requested.
Test nginx configuration with 'nginx -t' before reloading to avoid errors.
Reload nginx after changes to apply the new index settings without downtime.