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程序,天然沒法解析。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配置文件,結果以下html

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

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    }
相關文章
相關標籤/搜索