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.
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.
Using
nginx/0.7.65
The "garbling" you talk about is, as far as I can tell, a bug in nginx relating to nested location blocks (or maybe aliases in location blocks that do regex-based matching without a capture... I'm not sure). What I was able to do, however, was fairly straightforward.
First, you can put all your fastcgi parameters, including the
fastcgi_pass
line andfastcgi_param SCRIPT_FILENAME $request_filename
into a separate file for inclusion in the relevant parts of the site. I put mine in/etc/nginx/fragments/php
.Then, for
/foosite
, you need two location blocks, like so:One thing to be wary of here -- unlike "regular" location blocks, it appears that regex-based matching runs in the order specified in the config file (not longest-match-first, as appears to be the case for non-regex location blocks). So, if you're doing a site-specific PHP location, as well as a generic "all-site" PHP handler (
location ~ \.php$
) then you'll need to put the generic "all-site" handler last in the server block, or all hell will break loose.Yeah, this sucks, and if I get the motivation up I might try and work out exactly what's going wrong with the nested case (the config parser doesn't barf on it, so I suspect it's supposed to work but nobody actually uses it, so it's buggy).
AFAIK, you cannot use nested blocks.
Try something like the following instead.
You can modify the second block to be something like
(needs testing)