NGINX PHP FastCGI 多位置(别名)问题

NGINX PHP FastCGI multiple locations (alias) issue

第一次发帖,对这一切都很陌生。发现这里现有的指南很棒,但我正在努力让一些东西发挥作用。经过大量的反复试验并查看我的位置块目前看起来像这样 - 全局 PHP 已被删除并包含在我的位置块中。

第一个工作正常,经过一些更改后的第二个现在不显示 403 Forbidden 或 404 not found,而是显示通用字符串 'File not found.'

访问时我的Nginx错误日志显示如下:

2018/02/26 19:13:47 [error] 25229#25229: *185968 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: X.X.X.X, server: domain.co.uk, request: "GET /test/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domain.co.uk"

我一直在查看这里关于不同 fastcgi_param SCRIPT_FILENAME 参数的各种零碎信息,但我无法使它们中的任何一个起作用。任何建议将不胜感激,因为我花了数小时试图让它工作并取得了一些进展,但我认为这将是完成这项工作的最终任务。

我已经从别名中删除了 try_files,因为有人告诉我这不太好用(之前是 403),并将 $request_filename 添加到我的 fastcgi_param但这并没有阻止这个错误。

location ~ ^((?!\/test).)*$ {
include /etc/nginx/mime.types;
    root /var/www/html/test;
    index index.php index.html index.htm;
    location ~ \.php$ {
        root /var/www/html/test;
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}



location ~ ^/test(.*)$ {
include /etc/nginx/mime.types;
    alias /var/www/html/test2/;
    index index.php index.html index.htm;
    location ~ \.php$ {
        alias /var/www/html/test2/;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename;
        include fastcgi_params;
    }
}

对您的配置进行一些小调整。

  1. root /var/www/html/test; 移到第一个位置块之外。 nginx pitfalls
  2. 将第二块中的 alias 替换为 rootaccording to nginx docs
  3. 删除第二个块中的第二个别名。 反正是多余的
  4. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename; same error on serverfault
  5. 中删除 $request_filename;

基本上如下所示

 server {
   root /var/www/html/test;

   location /test {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location / {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }


}