nginx解析PHP程序報錯的解決方案

[root@server-2 log]# tail -f nginx/error.log
2019/07/31 01:51:02 [error] 26151#0: *52 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.1.124, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "192.168.1.136"
nginx解析PHP頁面時無法顯示出來,查看nginx日誌,報錯信息如上;
查看資料
原因:
在fastcgi_params文件(和nginx.conf文件在同一個目錄下)裏定義了許多與fastcgi程序相關的變量,當nginx通過fastcgi接口發送請求時它要去定義好的地方去解析相應的變量值,然後交給php去處理,很顯然,如果fastcgi獲取不了相應的值,那麼他就無法把相應請求轉發給php程序,自然無法解析。

這個問題的關鍵在於"Primary script unknown”主腳本未知,也就是fastcgi程序無法找到定義的要執行的腳本,默認的配置文件fastcgi_params定義的變量中並沒有$fastcgi_script_name這個變量,但是在fastcgi.conf裏定義了。所以我們要麼包含這個fastcgi.conf文件,要麼把fastcgi_params裏的SCRIPT_FILENAME 構建成和fastcgi.conf裏的值一樣,加上$document_root參數(這個參數的值就代表站點目錄root那一行的值)。
解決方案1
在配置最後一行包含fastcgi.conf配置文件,結果如下

 1    location ~ \.php$ {
 2             root           html/wordpress;
 3             fastcgi_pass   127.0.0.1:9000;
 4             fastcgi_index  index.php;
 5             fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 6             include        fastcgi_params;
 7             include        fastcgi.conf; 
 8     }

解決方案2
把fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;這一行,改爲:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

1     location ~ \.php$ {
2           root           html/wordpress;
3           fastcgi_pass   127.0.0.1:9000;
4            fastcgi_index  index.php;
5           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
6           include        fastcgi_params;
7    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章