What does try_files $uri $uri.html = @second do instead of try_files $uri $uri.html @second what is = for?
The directive syntax is:
try_files file1 file2 ... fallback;
- Nginx checks each
fileNin order (relative torootoralias). - If a match is found, it serves that file immediately.
- If none match, it processes the final argument:
-
If it starts with
=, it is an HTTP status code. -
Otherwise, it’s interpreted as an internal redirect to a URI (location block).
-
Let’s keep watch in your config file.
try_files $uri $uri.html = @second;
This means:
-
Check if there is a file at
$uri(e.g.,/about). -
If not, try
$uri.html(e.g.,/about.html). -
If neither exists → return HTTP status code
@second
← invalid, because @secondis not a number.
Actually, this won’t work. =something only accepts numeric codes (like =404, =403, =500).
try_files $uri $uri.html @second;
Check $uri.
- Check
$uri.html. - If neither exist → internally redirect to the named location
@second.
Our docs actually have quite a bit of documentation and examples for try_files.
The tl;dr is that the last parameter, uri or =code are used to determine what NGINX should do if the files are not found. If you use a “code”, NGINX will return that status code and the associated error page if there is one defined.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.