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的配置和优化

你可能感兴趣的:(web)