I want to fetch a file from the cache conditionally, based on a custom header in the request.
If the X-Proxy header is present in the request, return th
try_files directive only accepts one named location, so apparently it goes for the last one. This blog post proposes a workaround that works in your case. In case you don't won't read the whole post, you can add the following lines at the end of @local block:
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = @remote;
and change your try_files to this:
try_files /$uri @local;