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

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

第一次发帖,对这一切还很陌生 - 有人建议我通过 stack overflow 尝试一下,我读过一些不同的链接,但就是无法理解。经过多次尝试和查找,我的位置块目前看起来像这样 - 全局 PHP 已被删除并包含在我的位置块中。

第一个工作正常,第二个经过一些更改后现在不会显示 403 禁止或 404 未找到,而是显示通用字符串“未找到文件”。

www.mydomain.com- 提供文件 index.php/var/www/html/测试

www.mydomain.com/test- 应从 index.php 提供文件 /var/www/html/test2但失败并出现以下错误。

我的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 bu 但这并没有阻止这个错误。

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;
    }
}

推荐我在这里尝试的用户建议

Move the root /var/www/html/test; outside the first location block. nginx pitfalls
Replace alias with root in second block. according to nginx docs
remove the second alias in second block. it was redundant anyway
remove $request_filename; from fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename; same error on serverfault

我这样做了,然而这几乎是倒退了一步,导致在访问 /test/ 时出现 404 nginx 错误,他们建议的更改使其看起来像他们的例子那样 -

 server {
   root /var/www/html/test;
   location ~ ^((?!\/test).)*$ {
      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;
      }
   }

   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;
      }
   }
}

如果有人有任何知识可以帮助我,我很乐意与他们合作(我相信他们的胜过我自己)来尝试解决这个问题。

答案1

Stackoverflow 上的 Cemal 帮我解决了这个问题!

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;
      }
   }


}

404 的问题归结为第一个位置的文件位于 /var/www/html/test2,但位置 /test 带有前缀,因此目录在 Web 服务器端应该是 /var/www/html/test2。

相关内容