Directory fallback

merlin corey merlincorey at
Wed Dec 30 00:20:00 MSK 2009

On Tue, Dec 29, 2009 at 9:56 AM, Tobia Conforto
<tobia.conforto at> wrote:
> Hello
> I'm trying to come up with a nginx configuration file for a fallback-like directory structure.
> I have the following directories:
> /var/www/base/ (generic files)
> /var/www/site1/ (files specific to site1)
> Given a request for /logo.png, I would like nginx to do the following:
> 1. serve /var/www/site1/logo.png if exists; otherwise
> 2. serve /var/www/base/logo.png.
> The matter is complicated by the presence of PHP files. Given a request for /index.php (possibly in the same directory as image files...) nginx should:
> 1. serve /var/www/site1/index.php through FastCGI, if the php file exists; otherwise
> 2. serve /var/www/base/index.php through FastCGI.
> Can anybody please suggest the optimal combination of document_root, alias, location, rewrite, try_files or such options to use? I would like to avoid client-side redirects. Everything else is ok.
> Tobia
> _______________________________________________
> nginx mailing list
> nginx at

Make an attempt at least! :-)

I'll get you started... please note this is written for 0.8.30 and
above and I didn't test this, but it should be close.

upstream php {

server {
  listen 80;
  server_name site1;
  root /var/www/site1;
  fastcgi_intercept_errors on;
  location / {
    try_files $uri /var/www/base$uri;
  location ~ \.php {
    error_page 404 = @basephp
    include fastcgi.conf;
    fastcgi_pass php;
  location @basephp {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/base$fastcgi_script_name;
    fastcgi_pass php;

More information about the nginx mailing list