Proxy_pass only if a file exists

I need to make sure a file actually exists before proxy_pass-ing the
request
to an upstream server. I don’t serve existing files directly using Nginx
because there are some application-specific logic i need to perform on
the
application server for such requests.

I’ve looked at try_files, but it seems like it will serve the file
straightaway once it is found, which is not what I want here. Another
way
is to use if (!-f $request_filename), but as mentioned here:
http://wiki.nginx.org/Pitfalls#Check_IF_File_Exists, it’s not a terrible
way
to check the existence of a file.

Is there a feasible yet efficient way?

Posted at Nginx Forum:

Sorry, I meant to say that (!-f $request_filename) check IS a terrible
way
to check the existence of the file, as suggested by the documentation.

Posted at Nginx Forum:

On Tue, Apr 30, 2013 at 10:25:34AM -0400, mrtn wrote:

Hi there,

I need to make sure a file actually exists before proxy_pass-ing the request
to an upstream server. I don’t serve existing files directly using Nginx
because there are some application-specific logic i need to perform on the
application server for such requests.

I confess I don’t fully understand that – if file X exists on the local
filesystem, then ignore it and proxy_pass to server Y; but if it
doesn’t,
then don’t proxy_pass and do something else instead, which is presumably
better than caching the 404 from server Y.

But that’s ok; I don’t have to understand it.

I’ve looked at try_files, but it seems like it will serve the file
straightaway once it is found, which is not what I want here.

Correct. try_files serves the first file found, or else rewrites to the
final argument.

Another way
is to use if (!-f $request_filename), but as mentioned here:
Pitfalls and Common Mistakes | NGINX, it’s

a terrible way to check the existence of a file.

The entire purpose of “-f” is to check whether the thing named is a
file.

The common case is something like “if it is a file, serve it; else do
something different”, and now that try_files exists, “-f” is not the
best way to achieve the common case.

Is there a feasible yet efficient way?

It sounds like “-f” is what you want, perhaps following the example
shown on If is Evil… when used in location context | NGINX

f

Francis D. [email protected]