Nginx 可以处理别名内的 php(或类似的 fcgi)请求吗?

all*_*yin 4 php nginx fastcgi

我正在将旧的 Apache 服务器转换为 Nginx,并且没有更改 URL 或重新排列文件系统的奢侈。

是否可以在 Nginx 配置中使用嵌套的 location{} 块来告诉它在正常提供静态内容的同时将别名目录中的 .php 文件提供给 fastcgi?

与我失败的类似配置:

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

对 /foosite/static.jpg 的请求可以很好地提供服务,但是 nginx 在尝试将它们分派到 fastcgi 时似乎会混淆任何 .php 文件的路径。

小智 6

此处提供的解决方案不是解决方案。它不再正确了。使用 Lucid (10.4) 我能够使用这个解决方案。womble的解决方案的问题是没有DOCUMENT_ROOT正确设置参数;相反,它在 document_root 中包含脚本名称。

这似乎工作正常。

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;            
    }                                                 
}
Run Code Online (Sandbox Code Playgroud)

使用 nginx/0.7.65