Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

os: debian stable (squeeze) nginx: squeeze-backports 1.2.1-2~bpo60+1

try_files just behave strange to me.

this works as expected, $uri is tried localy if not found -> the @static location is used

try_files  $uri $uri/ @static;

just adding a =404 to the end leads to a 404. shouldn't it still work if i access a existing file which is located $uri or @static, and only serve a 404 if nothing is found on the @static location?

try_files  $uri $uri/ @static =404;

putting the =404 before the @static makes it work again. why? if the file is not located at the $uri or $uri/ location but on @static it should lead to 404 because =404 is befor the @static (for my understanding). if i put access a file which is located at $uri it is correctly served from nginx (not hitting the @static backend or the =404)

try_files  $uri $uri/ =404 @static ;

i am quite confused

edit: my goal would be: test if the file is locally, then check all upstream servers for the file and if not found on these locations return a 404


upstream domain.com_upstream
    server yyy.yyy.yyy.yyy:8000 weight=10 max_fails=3 fail_timeout=3s;
    server zzz.zzz.zzz.zzz:8020 weight=10 max_fails=3 fail_timeout=3s;


    listen    ;

    location @static
        proxy_ignore_client_abort off;
        proxy_intercept_errors    on;
        proxy_next_upstream       http_404 error timeout invalid_header;

        proxy_pass                  http://domain.com_upstream;
        proxy_redirect              off;
        proxy_set_header            Host                $host;
        proxy_set_header            X-Real-IP           $remote_addr;
        proxy_set_header            X-Forwarded-For     $proxy_add_x_forwarded_for;
        client_max_body_size        10m;
        client_body_buffer_size     128k;

        proxy_connect_timeout       10;
        proxy_send_timeout          120;
        proxy_read_timeout          120;

        proxy_buffers 8 16k;
        proxy_buffer_size 32k;

    location ~ ^/test 
        root /var/www/test;
        try_files $uri @static =404; # exchange with upper try_file examples...

    *** snip ***
share|improve this question
What is the desired behavior? – Michael Hampton Oct 15 '12 at 19:33
added the goal above – c33s Oct 15 '12 at 19:47
up vote 7 down vote accepted

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:

share|improve this answer

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

share|improve this answer

Your Answer


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.