2

I want all urls under /media/thumbnails to be served from disk, and if the file doesn't exist, from another server, but without redirecting the client. I believed this should work but I can't find what is wrong with it:

    location /media/thumbnails {
            try_files $uri @thumbnail_generator;
    }

    location /media {
            root /home/plinio/nginx_test;
            break;
    }

    location @thumbnail_generator {
            proxy_pass http://127.0.0.1:8081/media_generate$uri;
    }

Even after creating the /home/plinio/nginx_test/media/thumbnails/test.txt file, a request to /media/thumbnails/test.txt is still being internally redirected to @thumbnail_generator.

1 Answer 1

1

You haven't shown us your complete vhost configuration, so I can't say for sure, but your use of the root directive in the /media location block suggests that you might not have a root directive in the top-level vhost config.

Nginx location blocks are not cumulative; it finds the best single match location block (according to the rules specified in the documentation), and uses that. Hence, if you don't have one in the vhost config itself, your lookups for /media/thumbnails/text.txt aren't going to where you think they are on the filesystem, and hence the file doesn't exist and the internal redirect kicks in.

By the way, you can trivially find these sorts of problems by turning on debug logging and looking at the vhost error log. Nginx is quite good about logging what it's doing to process a request, and the completely wrong file path is usually a pretty big clue what you've done wrong.

1
  • I've changed my strategy and this is isn't a problem anymore, thanks for your help. I didn't fully solve this problem but it seems the issue was that I thought the "try_files $uri ..." directive would try to the use '/media' location first, and only on failure would use the @thumbnail_generator url. I believe a root directive before try_files would've solved it.
    – Plinio
    May 3, 2012 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .