Index directive in Nginx - Commands & Configuration
Start learning this pattern below
Jump into concepts and practice - no test required
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.
sudo nginx -t
sudo systemctl reload nginxcurl http://localhost/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.
Practice
What is the main purpose of the index directive in nginx?
Solution
Step 1: Understand the role of the index directive
The index directive tells nginx which files to look for by default when a user requests a directory URL.Step 2: Match the purpose with the options
Only To specify default files to serve when a directory is requested describes setting default files to serve in a folder, which matches the index directive's function.Final Answer:
To specify default files to serve when a directory is requested -> Option AQuick Check:
index directive = default files [OK]
- Confusing index with server IP settings
- Thinking index sets error pages
- Mixing index with SSL configuration
Which of the following is the correct syntax to set index.html and home.html as default files using the index directive?
?
Solution
Step 1: Recall nginx index directive syntax
The correct syntax lists files separated by spaces, ending with a semicolon.Step 2: Compare options with correct syntax
index index.html home.html; matches the correct syntax:index index.html home.html;. Others use invalid punctuation or braces.Final Answer:
index index.html home.html; -> Option DQuick Check:
Correct syntax = index index.html home.html; [OK]
- Using commas between filenames
- Using braces or colons incorrectly
- Adding equal signs in directive
Given this nginx config snippet:
location / {
index about.html index.html;
}If the folder contains index.html but not about.html, which file will nginx serve when a user visits /?
Solution
Step 1: Understand index file priority
nginx tries files in order: firstabout.html, thenindex.html.Step 2: Check which files exist
about.htmlis missing, butindex.htmlexists, so nginx servesindex.html.Final Answer:
index.html -> Option CQuick Check:
First found file served = index.html [OK]
- Assuming nginx serves first listed file regardless of existence
- Expecting directory listing if first file missing
- Thinking nginx returns error immediately
Identify the error in this nginx config snippet:
location / {
index index.html, home.html;
}Solution
Step 1: Check syntax for index directive
File names must be separated by spaces, not commas.Step 2: Identify the error in the snippet
The comma betweenindex.htmlandhome.htmlis invalid syntax.Final Answer:
Using a comma between filenames is invalid syntax -> Option BQuick Check:
No commas allowed in index list [OK]
- Adding commas between filenames
- Omitting semicolon
- Thinking quotes are required
You want nginx to serve main.html as the default file, but only if index.html is missing. Which index directive correctly achieves this?
Solution
Step 1: Understand index file priority order
nginx serves the first existing file in the list from left to right.Step 2: Choose order to serve main.html only if index.html missing
To servemain.htmlonly ifindex.htmlis missing,index.htmlmust be first, thenmain.html.Final Answer:
index index.html main.html; -> Option AQuick Check:
First existing file served = index.html then main.html [OK]
- Reversing file order
- Listing only one file
- Expecting nginx to skip files in order
