Nginx – Can Nginx handle php (or similar fcgi) requests inside of an alias

fastcginginxPHP

I am converting a legacy Apache server to Nginx and do not have the luxury of changing URL's or rearranging the filesystem.

Is it possible to use nested location{} blocks in the Nginx configuration to tell it to feed the .php files in an aliased directory to fastcgi while serving static content normally?

Similar configuration to what fails me:

server {
  listen 80;

  location / {
    index  index.html;
  }

  location /foosite/ {
    alias  /var/aliases/foo;
    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
  }
}

Requests for /foosite/static.jpg are served fine, but nginx appears to garble the path to any .php files when attempting to dispatch them to fastcgi.

Best Answer

The solution provided here is not a solution. And it's not correct anymore. Using Lucid (10.4) I was able to use this solution. The problem with womble's solution is that it doesn't set the DOCUMENT_ROOT parameter properly; rather, it includes the script name in the document_root.

This seems to work OK.

location /foosite {
    alias /home/foosite/www/;
    index index.php index.html index.htm;

    location ~ /foosite/(.*\.php)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$1;
        include /etc/nginx/fastcgi_params;            
    }                                                 
}

Using nginx/0.7.65