Nginx try_files and index: Check Files in Order
Choose a missing-file policy
try_files lets a location test candidate filesystem paths in sequence. For this lesson assume a normal root-based static location, no rewrite rules and a private public-content tree. A successful candidate continues within the current location. The last argument is a fallback action, not an ordinary existence check.
For /assets/theme.css, Nginx checks site/assets/theme.css. If it exists and is readable, the static handler serves it. If the check finds no candidate, =404 requests a not-found response. A missing CSS file should not silently return an HTML page.
A directory is not an index file
location /guide/ { try_files $uri $uri/ =404; index welcome.html index.html; autoindex off;}Here $uri/ requests a directory check. For /guide/, the directory can exist even if no welcome page exists. Index processing checks the configured names for slash-ended requests. If site/guide/welcome.html exists, index processing internally selects /guide/welcome.html; that path may match another location. A directory without a matching index, with directory listing disabled and otherwise accessible, normally produces 403 in this static setup. A nonexistent directory is different: it fails the existence checks.
/guide/ 200 welcome.html/guide/welcome.html 200 welcome.html/guide/empty/ 403 no index or listing/guide/absent/ 404 no candidateDo not interpret every 403 as an ownership problem. Directory-index policy is also a possible cause. Requests without a trailing slash may be redirected by the static handler when a directory is found; this fixture tests slash-ended directory requests explicitly.
A fallback can select another URI or named handler
location /thumbs/ { try_files $uri /images/logo.svg;}location /images/ { try_files $uri =404;}A missing thumbnail is internally routed to the known public logo. The browser does not need to request a second URL. This is reasonable only if the product intends a placeholder; for JavaScript, CSS or API data it may hide a broken resource. The fallback target must be valid and must not lead back into an endless fallback.
location /notes/ { try_files $uri @note_missing;}location @note_missing { default_type text/plain; return 404 "note not found\n";}The @ name points to a handler inside the same server rather than a URL for a client to visit. An explicit 404 here keeps the not-found meaning. In more complex alias or regex locations, verify path construction and internal redirects in the actual selected build rather than assuming root-style calculations apply unchanged.
Predict before running
curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18089/guide/curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18089/assets/missing.cssExpect an HTML welcome page and a 404 respectively. The next lesson separates custom error presentation from client-side application routes.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
File candidates and directory indexes
Predict the file or response and explain the selected behavior.
What does the final =404 in try_files $uri =404 do?
What does the trailing slash in $uri/ check?
The guide directory exists but has no index and autoindex is off. What is the normal response for /guide/empty/ in this fixture?
Why may an index file select another location?
What is @note_missing in the lesson example?