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

To be more performant, we'd like to allow nginx to catch 404s before passing it off to the upstream apache server. Both servers have the same files.

More Info

We have a popular setup of serving static content on nginx and using apache upstream to serve dynamic content. We currently sync php along with static files to the nginx nodes from the upstream apache nodes. The same files are located on both layers. We simply serve only static files from nginx.

Currently, all non-static requests go upstream to the apache server (as previously intended). However, I realized nginx should be able to check for the existence of a .php file before deciding to pass it on upstream. That would mean handling a request much more efficiently.

I've been tinkering with a combination of try_files, then eventually if blocks to find a solution, but I haven't been successful. Please advise.


share|improve this question

migrated from Jun 7 '11 at 14:30

This question came from our site for professional and enthusiast programmers.

why not just turn on the proxy_cache and cache 404s for some period of time? – polynomial Sep 18 '11 at 5:13

There is a pretty simple solution:

server {
    access_log  /var/log/nginx/access.log main;

    root /var/www/;

    location / {
        # If the file exists as a static file serve it directly without
        # running all the other rewite tests on it
        if (-f $request_filename) { 


please +1 if it helped ;) thanks

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.