Bird
Raised Fist0
Nginxdevops~5 mins

Index directive in Nginx - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the index directive in nginx?
The index directive tells nginx which file to serve when a directory is requested. It looks for the specified files in order and serves the first one it finds.
Click to reveal answer
beginner
How does nginx behave if the index directive is not set and a directory is requested?
If the index directive is not set, nginx uses the default value index.html and attempts to serve that file for a directory request. If the file does not exist, it may return a 403 Forbidden error or list the directory contents if autoindex is enabled.
Click to reveal answer
beginner
Example:
index index.html index.htm;
What does this configuration mean?
This means nginx will first look for a file named index.html in the requested directory. If it is not found, it will look for index.htm. It serves the first file found.
Click to reveal answer
intermediate
Can the index directive accept multiple file names? How does nginx use them?
Yes, the index directive can list multiple file names separated by spaces. Nginx checks them in order and serves the first existing file it finds in the directory.
Click to reveal answer
intermediate
Where in the nginx configuration file can the index directive be placed?
The index directive can be placed inside the http, server, or location blocks to control default files at different levels.
Click to reveal answer
What does the nginx index directive do?
ASpecifies default files to serve when a directory is requested
BDefines the server's IP address
CSets the maximum file upload size
DConfigures SSL certificates
If index index.html index.htm; is set, and both files exist, which one will nginx serve?
Aindex.htm
BBoth files simultaneously
Cindex.html
DIt will return an error
Where can the index directive be used in nginx configuration?
AOnly inside <code>server</code> block
BInside <code>http</code>, <code>server</code>, or <code>location</code> blocks
COnly inside <code>http</code> block
DOnly inside <code>location</code> block
What happens if no file listed in the index directive exists in the directory?
ANginx lists the directory contents if autoindex is enabled
BNginx serves the first file anyway
CNginx serves a 404 Not Found error
DNginx restarts automatically
Can the index directive accept only one file name?
ANo, it requires at least two file names
BYes, but only if the file is named index.html
CNo, it only accepts directories
DYes, it can accept one or more file names
Explain how the nginx index directive works when a user requests a directory URL.
Think about what nginx does when you visit a folder on a website.
You got /4 concepts.
    Describe where you can place the index directive in nginx configuration and why placement matters.
    Consider different levels of configuration in nginx.
    You got /4 concepts.

      Practice

      (1/5)
      1.

      What is the main purpose of the index directive in nginx?

      easy
      A. To specify default files to serve when a directory is requested
      B. To set the server's IP address
      C. To configure SSL certificates
      D. To define error pages

      Solution

      1. 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.
      2. 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.
      3. Final Answer:

        To specify default files to serve when a directory is requested -> Option A
      4. Quick Check:

        index directive = default files [OK]
      Hint: Index sets default homepage files for folders [OK]
      Common Mistakes:
      • Confusing index with server IP settings
      • Thinking index sets error pages
      • Mixing index with SSL configuration
      2.

      Which of the following is the correct syntax to set index.html and home.html as default files using the index directive?

      ?
      easy
      A. index: index.html; home.html;
      B. index = index.html, home.html;
      C. index { index.html home.html }
      D. index index.html home.html;

      Solution

      1. Step 1: Recall nginx index directive syntax

        The correct syntax lists files separated by spaces, ending with a semicolon.
      2. 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.
      3. Final Answer:

        index index.html home.html; -> Option D
      4. Quick Check:

        Correct syntax = index index.html home.html; [OK]
      Hint: List files with spaces, end with semicolon [OK]
      Common Mistakes:
      • Using commas between filenames
      • Using braces or colons incorrectly
      • Adding equal signs in directive
      3.

      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 /?

      medium
      A. 404 Not Found error
      B. about.html
      C. index.html
      D. Directory listing

      Solution

      1. Step 1: Understand index file priority

        nginx tries files in order: first about.html, then index.html.
      2. Step 2: Check which files exist

        about.html is missing, but index.html exists, so nginx serves index.html.
      3. Final Answer:

        index.html -> Option C
      4. Quick Check:

        First found file served = index.html [OK]
      Hint: nginx serves first existing file in index list [OK]
      Common Mistakes:
      • Assuming nginx serves first listed file regardless of existence
      • Expecting directory listing if first file missing
      • Thinking nginx returns error immediately
      4.

      Identify the error in this nginx config snippet:

      location / {
          index index.html, home.html;
      }
      medium
      A. Missing semicolon at the end
      B. Using a comma between filenames is invalid syntax
      C. index directive cannot be used inside location block
      D. File names must be in quotes

      Solution

      1. Step 1: Check syntax for index directive

        File names must be separated by spaces, not commas.
      2. Step 2: Identify the error in the snippet

        The comma between index.html and home.html is invalid syntax.
      3. Final Answer:

        Using a comma between filenames is invalid syntax -> Option B
      4. Quick Check:

        No commas allowed in index list [OK]
      Hint: Separate files with spaces, no commas [OK]
      Common Mistakes:
      • Adding commas between filenames
      • Omitting semicolon
      • Thinking quotes are required
      5.

      You want nginx to serve main.html as the default file, but only if index.html is missing. Which index directive correctly achieves this?

      hard
      A. index index.html main.html;
      B. index main.html index.html;
      C. index main.html;
      D. index index.html;

      Solution

      1. Step 1: Understand index file priority order

        nginx serves the first existing file in the list from left to right.
      2. Step 2: Choose order to serve main.html only if index.html missing

        To serve main.html only if index.html is missing, index.html must be first, then main.html.
      3. Final Answer:

        index index.html main.html; -> Option A
      4. Quick Check:

        First existing file served = index.html then main.html [OK]
      Hint: List index.html first to prefer it over main.html [OK]
      Common Mistakes:
      • Reversing file order
      • Listing only one file
      • Expecting nginx to skip files in order