[PHP] 安裝PHP報錯「Primary script unknown」的解決方法

  • 2019 年 10 月 11 日
  • 筆記

當安裝完最新版的nginx和最新版的PHP以後,使用niginx默認配置文件中的PHP配置會有錯誤 訪問指定目錄下的php文件時,顯示File not found.錯誤。查看nginx的錯誤日誌顯示

90#90: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1"

排查的過程: 1.修改默認PHP配置中的fastcgi_param配置fastcgi_param配置,這個地方默認的是/Scripts$fastcgi_script_name

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

2.把root指令從location /{}塊裡面拿到外面,因為看fpm的access日誌,訪問PHP文件報的404錯誤

最簡配置如下:

server {      listen       80;      server_name  127.0.0.1;      access_log  /var/log/nginx/default.access.log  main;      error_log /var/log/nginx/default.error.log;      root   /var/www/html;      location / {          index  index.html index.htm;      }      location ~ .php$ {          fastcgi_pass   127.0.0.1:9000;          fastcgi_index  index.php;          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;          include        fastcgi_params;      }  }