0
0
Nginxdevops~20 mins

Try_files directive in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.