Recall & Review
beginner
What does the
try_files directive do in nginx?It checks for the existence of files or directories in order, and serves the first one found. If none are found, it can redirect to a fallback URI or return an error.
Click to reveal answer
beginner
How do you write a
try_files directive to check for index.html and then fallback to index.php?try_files /index.html /index.php;
Click to reveal answer
beginner
What happens if none of the files in
try_files exist and no fallback is specified?nginx returns a 404 Not Found error by default.
Click to reveal answer
intermediate
Why is
try_files preferred over if statements for checking file existence in nginx?try_files is more efficient and designed for this purpose, avoiding complex and error-prone if conditions.Click to reveal answer
intermediate
Give an example of a
try_files directive that tries $uri, $uri/, and then redirects to /index.php with query string.try_files $uri $uri/ /index.php?$query_string;
Click to reveal answer
What does
try_files $uri /fallback.html; do?✗ Incorrect
The directive tries to serve the file at $uri first. If it doesn't exist, it serves /fallback.html.
If
try_files does not find any file and no fallback is given, what is the default response?✗ Incorrect
nginx returns a 404 Not Found error by default when no file is found and no fallback is specified.
Which is a valid use of
try_files?✗ Incorrect
Option A is the correct syntax for try_files. The others are invalid.
Why should
try_files be used instead of if to check file existence?✗ Incorrect
try_files is optimized for checking files and is more efficient than if statements.What does this directive do?
try_files $uri $uri/ =404;✗ Incorrect
It tries to serve $uri, then $uri/ (directory), and returns a 404 error if neither exists.
Explain how the
try_files directive works in nginx and why it is useful.Think about how nginx decides which file to serve when multiple options exist.
You got /4 concepts.
Write a
try_files directive that tries the requested URI, then the URI as a directory, and finally redirects to /index.php with query parameters.Use variables $uri and $query_string in the directive.
You got /4 concepts.