Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I looked at the nginx documentation and it still confuses me utterly

How does try_files work? Here is what the document says

From NginxHttpCoreModule

try_files

syntax: try_files path1 [path2] uri

default: none

context: server, location

availability: 0.7.27

Checks for the existence of files in order, and returns the first file that is found. A trailing slash indicates a directory - $uri /. In the event that no file is found, an internal redirect to the last parameter is invoked. The last parameter is the fallback URI and must exist, or else an internal error will be raised. Unlike rewrite, $args are not automatically preserved if the fallback is not a named location. If you need args preserved, you must do so explicitly:

I dont understand how it checks the paths and what if i dont want a internal error and have it resume the rest of the path in effort to find another file?

If i have a cache file at /path/app/cache/url/index.html and if it fails try /path/app/index.php how do i write that? if i wrote

    try_files /path/app/cache/ $uri
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;

i have index index.php index.html index.htm; when i visit /urlname will it try checking /path/app/cache/urlname (index.php then) /path/app/cache/urlname/index.html? if we ignore everything after try_files is it possibly for try_files to check this cache folder? i been trying and failed.

share|improve this question

1 Answer 1

up vote 15 down vote accepted

try_files tries the literal path you specify in relation to the defined root directive and sets the internal file pointer. If you use for instance try_files /app/cache/ $uri @fallback; with index index.php index.html; then it will test the paths $document_root/app/cache/index.php, $document_root/app/cache/index.html and $document_root$uri before finally internally redirecting to the @fallback named location. You can also use a file or a status code (=404) as your last parameter but if using a file it must exist.

You should note that try_files itself will not issue an internal redirect for anything but the last parameter. Meaning you cannot do the following: try_files $uri /cache.php @fallback; as that will cause nginx to set the internal file pointer to $document_root/cache.php and serve it, but since no internal redirect takes place the locations aren't re-evaluated and as such it will be served as plain text. (The reason it works with PHP files as the index is that the index directive will issue an internal redirect)

share|improve this answer
    
That is MUCH more clear. Thanks. I'm a little unsure how the named location works. If @fallback has lines for fastcgi php that would serve it as a php file rather then text? Is fallback used when everything before it fails? –  acidzombie24 Nov 11 '11 at 5:13
1  
A named location is just functionally identical to a normal location except it can only be accessed via internal mechanisms such as error_page and try_files. The fallback in try_files is only used when none of the specified paths result in a valid file. You still need a location to catch \.php$ URIs as otherwise try_files will trigger on $uri if the file exist and serve it as plain-text. –  Martin Fjordvald Nov 11 '11 at 20:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.