无法在Nginx中将别名与fastcgi_cache结合使用

时间:2013-08-26 17:23:52

标签: php nginx

这有效:

location ~ ^/special/(.+\.php)$ {
  alias /var/special/$1;
  try_files "" =404;
  include fastcgi_params;
  fastcgi_pass 127.0.0.1:9000; # php-fpm socket
}

但这不是:

location ~ ^/special/(.+\.php)$ {
  alias /var/special/$1;
  try_files "" =404;
  include fastcgi_params;
  fastcgi_pass 127.0.0.1:9000; # php-fpm socket
  fastcgi_cache mycache;
}

如果我尝试访问网址“/special/index.php”,我会收到“找不到文件”。浏览器中的文本,我假设它来自php-fpm或PHP。我在Nginx日志中得到了这个错误:

FastCGI sent in stderr: "Primary script unknown", client: 202.179.27.65, server: myserver.org, request: "GET /special/index.php HTTP/1.1", host: "myserver.org"

任何想法为什么添加fastcgi_cache会打破这个?

请注意,当我使用使用别名的位置时,fastcgi_cache正常工作。

1 个答案:

答案 0 :(得分:0)

经过几天(!)摆弄它,这种变化似乎才能发挥作用:

location ~ ^/special(/.+\.php)$ {
  root /var/special;
  try_files "" =404;
  include fastcgi_params;
  fastcgi_pass 127.0.0.1:9000; # php-fpm socket
  fastcgi_cache mycache;
  fastcgi_param SCRIPT_FILENAME $document_root$1;
  fastcgi_param SCRIPT_NAME $1;
}

似乎有所不同的是1)使用“root”,这似乎是fastcgi_cache所需要的,2)显式设置SCRIPT_FILENAME和SCRIPT_NAME,否则“root”将无效(即使没有fastcgi_cache)