What is the try_files directive in Nginx and how does it work?
Understand the Nginx try_files directive: how it tests paths in order, serves the first match, and falls back to a URI or 404, with SPA and static examples.
Expected Interview Answer
try_files tells Nginx to test a list of file or directory paths in order and serve the first one that exists, falling back to a final URI or an internal redirect (or a status code) if none match.
It is most often written as try_files $uri $uri/ /index.html; — Nginx checks the exact file, then the directory, and finally hands off to a fallback route. The last argument is special: a path or named location triggers an internal redirect, while =404 (or =403 etc.) returns that status. This makes it the standard way to serve static assets while routing everything else to a single-page app entry point or a backend handler.
- Serves existing files without extra processing
- Provides clean fallback for SPA and pretty URLs
- Avoids costly if blocks for existence checks
- Runs internally with no extra client round trip
- Lets you return explicit status codes when nothing matches
AI Mentor Explanation
A captain has a batting order: try the opener, if they are out try the next, then the next, and if all fail send in the designated fallback batter. try_files works the same way — Nginx tries each path in order and serves the first that exists, and the last name in the list is the guaranteed fallback player who always comes in when nobody before them is available.
Step-by-Step Explanation
Step 1
List paths in priority order
Write try_files with the most specific path first, e.g. try_files $uri $uri/ ...; so exact files win.
Step 2
Nginx tests each path
For every argument except the last, Nginx checks if that file or directory exists on disk relative to root.
Step 3
Serve the first match
The first existing path is served directly, with no further processing of later arguments.
Step 4
Apply the fallback
If nothing exists, the last argument runs: a URI or named location triggers an internal redirect; =CODE returns that status.
Step 5
Route to app or 404
Point the fallback at /index.html for SPAs, at a backend location for APIs, or use =404 to signal a missing resource.
What Interviewer Expects
- Defines try_files as ordered existence checks
- Knows the first existing path is served
- Explains the special last argument behavior
- Distinguishes internal redirect from =404
- Gives the SPA fallback pattern try_files $uri $uri/ /index.html
- Notes it avoids if-based existence checks
Common Mistakes
- Thinking every argument is served rather than just the first match
- Forgetting the last argument is a fallback, not a normal file check
- Using =404 when an internal redirect to /index.html was intended
- Placing try_files so root is undefined and paths resolve wrongly
- Overusing if blocks instead of try_files for file existence
Best Answer (HR Friendly)
“try_files makes Nginx check a list of files in order and serve the first one it finds. If none exist, it uses a final fallback, which is how sites serve real files but send everything else to a single app page or a not-found response.”
Code Example
location / {
# serve the file, then the directory, else the app entry point
try_files $uri $uri/ /index.html;
}
location /images/ {
# serve the image or return an explicit 404
try_files $uri =404;
}Follow-up Questions
- How does the last argument of try_files differ from the others?
- How would you route try_files to a backend proxy instead of a file?
- Why is try_files preferred over if for checking file existence?
- What does try_files $uri $uri/ =404 do?
- How does try_files interact with the root directive?
MCQ Practice
1. What does try_files serve when several listed paths exist?
try_files serves the first path in the list that exists and ignores the rest.
2. In try_files $uri $uri/ /index.html; what is /index.html?
The final argument is a fallback URI that triggers an internal redirect when no earlier path exists.
3. How do you make try_files return a 404 when nothing matches?
A final argument of =404 makes Nginx return a 404 status when no listed file or directory exists.
Flash Cards
What does try_files do? — Tests a list of paths in order and serves the first that exists, using the last argument as a fallback.
What is special about the last try_files argument? — It is a fallback: a URI or named location triggers an internal redirect, while =CODE returns that status.
SPA fallback pattern? — try_files $uri $uri/ /index.html; serves real files, else routes to the app entry point.
Why prefer try_files over if? — It checks file existence internally and efficiently, avoiding the pitfalls of if blocks in location context.