1. 程式人生 > >FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream 問題解決

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream 問題解決

配置好了 nginx.conf 和 php7.0-fpm.conf 檔案,但是要訪問 php 檔案的時候,卻不顯示任何內容或者顯示“File not found”,問題在於要訪問的 php 檔案 php7.0-fpm 沒有訪問許可權,修改下訪問許可權即可。

改成如下圖所示:

附 nginx.conf, fastcgi.conf 和 php7.0-fpm.conf 配置:

在 nginx.conf 中,一定要配置好 root 否則也會報上面的錯誤。因為 fastcgi.conf 裡面用到了 $document_root 這一 nginx 變數!

# nginx.conf

worker_processes  4;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8090;

        root /home/drelang/myweb/;

        location =/ {
            default_type text/html;
            root /home/drelang/myweb/;
            index index.html;
            autoindex on;
        }

        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            include fastcgi.conf;
        }
    }
}
# fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;                                    
# php-fpm.conf

pid = /run/php/php7.0-fpm.pid
error_log = /var/log/php7.0-fpm.log

user = www-data
group = drelang

listen = /run/php/php7.0-fpm.sock

listen.owner = www-data
listen.group = drelang

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

由於我的 nginx 的使用者為 drelang ,因此要使得 nginx 對 php7.0-fpm.sock 檔案有讀寫許可權,就需要將 php-fpm.conf 中的 listen.group 和 listen.user 改為 drelang ,不然會報錯的。同時,listen.mode = 0660 開啟!

php-fpm.conf 常用配置可以參考以下博文:

php-fpm的配置和優化