Strange behavior with nginx try_files. What's wrong in my config?

Solution 1:

You misunderstood try_files parameters. Quote from docs:

If none of the files were found, an internal redirect to the uri specified by the last parameter is made.

Only the last parameter is a fallback URI (or a named location, or a code), all other parameters are files to test. I.e.

try_files /file1 /file2 /file3 ... @fallback;

will check for files under the document root (/file1, /file2, /file3 and so on), and if nothing found nginx will do an internal redirect to a @fallback.

The try_files ... @static =404; doesn't really make sense as it will test for a file named @static under the document root, and most likely it's not what you want. The try_files ... =404 @static; doesn't make sense, too, as it will test for a file named =404.

See here for docs: http://nginx.org/r/try_files.

Solution 2:

for completness (answer from kolbyjack @ irc #nginx):

kolbyjack: You only get one fallback. Whichever of @static or =404 is first is tested for existence as a file. which will fail, and nginx moves on to the fallback

c33s: i am not sure i understand, if i define multiple try_file options they are not tested from left to right?

kolbyjack: Right, but all except the last are tested as files on disk. So try_files $uri @static =404; looks for $document_root$uri, then $document_root@static, and if neither of those exist, then is issues a 404. try_files $uri =404 @static looks for $document_root$uri, then $document_root=404, and if neither of those exist, internally redirects to @static

c33s: so if @static is not a location on the local filesystem it only makes sense as last option?

kolbyjack: Correct Everything but the last arg should be things you want to test for existence on disk

c33s: do you want to answer on serverfault to get the rep? if not i would copy your answer to serverfault and answer my question myself...

kolbyjack: Go ahead, I'm too busy