Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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.

Thanks!

share|improve this question

migrated from stackoverflow.com 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

1 Answer 1

There is a pretty simple solution:

server {
    listen      0.0.0.0;
    server_name fooo.org;
    access_log  /var/log/nginx/access.log main;

    root /var/www/fooo.org;

    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) { 
            break; 
        }

        proxy_pass http://127.0.0.1:8080;
    }
}

please +1 if it helped ;) thanks

share|improve this answer

Your Answer

 
discard

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.