Bird
Raised Fist0
Nginxdevops~20 mins

Try_files directive in Nginx - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Try_files Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What happens when try_files finds the first file?
Given this nginx configuration snippet:
location /images/ {
  try_files $uri /default.jpg;
}

What will nginx serve if the requested file /images/logo.png exists on the server?
Nginx
location /images/ {
  try_files $uri /default.jpg;
}
AIt serves the file /default.jpg
BIt returns a 404 Not Found error
CIt serves the file /images/logo.png
DIt redirects to /default.jpg
Attempts:
2 left
💡 Hint
Try_files checks each file in order and serves the first one that exists.
💻 Command Output
intermediate
1:30remaining
What is the output when all try_files options fail?
Consider this nginx config:
location /docs/ {
  try_files $uri /index.html =404;
}

If the requested file /docs/manual.pdf does not exist, what will nginx do?
Nginx
location /docs/ {
  try_files $uri /index.html =404;
}
ARedirect to /index.html
BReturn a 404 Not Found error
CServe /docs/manual.pdf anyway
DServe /index.html
Attempts:
2 left
💡 Hint
Try_files tries each option in order until one exists or a fallback is reached.
Troubleshoot
advanced
2:00remaining
Why does try_files cause a 404 error unexpectedly?
You have this config:
location /app/ {
  try_files $uri $uri/ /app/index.php?$args;
}

Requests to /app/dashboard return 404 errors even though index.php exists. What is the likely cause?
Nginx
location /app/ {
  try_files $uri $uri/ /app/index.php?$args;
}
AThe location block must include 'index index.php;' directive
BThe try_files fallback does not trigger internal redirect properly without 'last' or 'break'
CThe query string in fallback is ignored, causing 404
DThe last parameter should start with an equal sign (=) to avoid 404
Attempts:
2 left
💡 Hint
Try_files fallback can cause 404 if internal redirect is not handled correctly.
Best Practice
advanced
1:30remaining
Which try_files usage is best for SPA (Single Page Application)?
You want nginx to serve static files if they exist, but fallback to /index.html for all other routes in a SPA. Which try_files directive is best?
Atry_files $uri $uri/ /index.html;
Btry_files /index.html $uri $uri/;
Ctry_files $uri /index.html =404;
Dtry_files $uri /index.html /404.html;
Attempts:
2 left
💡 Hint
Try_files should check static files first, then fallback to index.html for SPA routing.
🧠 Conceptual
expert
2:00remaining
How does try_files interact with alias directive?
Given this config:
location /static/ {
  alias /var/www/assets/;
  try_files $uri /fallback.png;
}

If a client requests /static/logo.png, which file path does try_files check first?
Nginx
location /static/ {
  alias /var/www/assets/;
  try_files $uri /fallback.png;
}
AIt checks /var/www/assets/static/logo.png
BIt checks /var/www/assets/logo.png
CIt checks /static/logo.png on the server root
DIt checks /var/www/assets/fallback.png
Attempts:
2 left
💡 Hint
Try_files uses the URI, not the alias path, when checking files.

Practice

(1/5)
1. What is the main purpose of the try_files directive in nginx?
easy
A. To check multiple files or paths in order and serve the first found
B. To restart the nginx server automatically
C. To define user permissions for files
D. To log errors in a custom file

Solution

  1. Step 1: Understand the role of try_files

    The try_files directive checks a list of files or paths one by one.
  2. Step 2: Identify its purpose

    It serves the first file found or falls back to a default if none exist.
  3. Final Answer:

    To check multiple files or paths in order and serve the first found -> Option A
  4. Quick Check:

    try_files = check files in order [OK]
Hint: Remember: try_files tries files one by one until success [OK]
Common Mistakes:
  • Thinking try_files restarts nginx
  • Confusing try_files with permission settings
  • Assuming try_files logs errors
2. Which of the following is the correct syntax for the try_files directive in nginx?
easy
A. try_files file1 file2 fallback;
B. try_files file1 file2 =404;
C. try_files file1, file2, fallback;
D. try_files (file1 file2 fallback);

Solution

  1. Step 1: Recall try_files syntax rules

    The try_files directive lists files separated by spaces, ending with a fallback like =404.
  2. Step 2: Identify correct syntax

    try_files file1 file2 =404; uses spaces and ends with =404, which is valid syntax.
  3. Final Answer:

    try_files file1 file2 =404; -> Option B
  4. Quick Check:

    try_files syntax uses spaces and fallback [OK]
Hint: Use spaces, no commas or parentheses, end with fallback [OK]
Common Mistakes:
  • Using commas between file names
  • Adding parentheses around files
  • Omitting fallback or using wrong fallback syntax
3. Given this nginx config snippet:
location / {
  try_files $uri $uri/ /index.html;
}

What happens when a user requests /about and /about is not a file but /about/ is a directory?
medium
A. nginx serves the /about file
B. nginx returns a 404 error
C. nginx serves the /about/ directory index
D. nginx serves /index.html fallback

Solution

  1. Step 1: Understand try_files order

    nginx first checks if /about file exists, then /about/ directory.
  2. Step 2: Apply to given request

    /about file is missing, but /about/ directory exists, so nginx serves the directory index.
  3. Final Answer:

    nginx serves the /about/ directory index -> Option C
  4. Quick Check:

    try_files checks files then directories [OK]
Hint: try_files checks files then directories in order [OK]
Common Mistakes:
  • Assuming it serves /index.html fallback immediately
  • Thinking it returns 404 if file missing
  • Ignoring directory check after file check
4. Identify the error in this nginx config snippet:
location / {
  try_files $uri, $uri/, /fallback.html;
}
medium
A. Using commas between file names is invalid syntax
B. Missing semicolon at the end of try_files
C. Fallback file must be =404, not /fallback.html
D. Variables like $uri cannot be used in try_files

Solution

  1. Step 1: Check try_files syntax

    try_files expects file names separated by spaces, not commas.
  2. Step 2: Identify error in snippet

    The commas after $uri and $uri/ cause syntax error.
  3. Final Answer:

    Using commas between file names is invalid syntax -> Option A
  4. Quick Check:

    try_files uses spaces, no commas [OK]
Hint: No commas in try_files list, only spaces [OK]
Common Mistakes:
  • Adding commas between file names
  • Confusing fallback with error codes
  • Thinking variables are disallowed in try_files
5. You want nginx to try serving /images/$uri, then /images/default.png if the first doesn't exist. Which try_files directive correctly implements this?
hard
A. try_files /images/$uri /images/default.png /index.html;
B. try_files $uri /images/default.png;
C. try_files /images/$uri /images/default.png=404;
D. try_files /images/$uri /images/default.png;

Solution

  1. Step 1: Understand file paths in try_files

    To check /images/$uri first, it must be the first argument.
  2. Step 2: Choose fallback file

    If the first file is missing, serve /images/default.png as fallback.
  3. Step 3: Validate options

    try_files /images/$uri /images/default.png; matches this logic exactly without extra fallbacks or error codes.
  4. Final Answer:

    try_files /images/$uri /images/default.png; -> Option D
  5. Quick Check:

    try_files lists files in order, fallback last [OK]
Hint: List files in order, fallback last without extra codes [OK]
Common Mistakes:
  • Using $uri without /images/ prefix
  • Adding unnecessary fallback like =404
  • Ending with unrelated fallback like /index.html