nginx中Primaryscriptunknown问题解决

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

问题解决

错误日志里报如下错误:

2019/05/12 17:46:09 [error] 19747#0: *39351836 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: t: 192.168.1.202, se, server: r: c.bailitop.com, re, request: "POST /plus/us/read.php HTT HTTP/1.0", upstream: "fastcgi://://127.0.0.1:9000", h", host: ": "c.bailitop.com", r", referrer: ": "http://c.bailitop.com/lequ.php""  

这种情况分为两种情况:

打开网址出 404 File not found. 错误,但文件的确是存在的,找查nginx error log,发现这个

2019/05/12 00:01:01 [error] 24257#0: *166460 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 223.73.106.97, server: c.bailitop.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "c.bailitop.com"

这个是因为nginx配置文件中fastcgi_param没有设置对路径引起的,在nginx的 default.conf 配置文件中有这么一段:

    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

复制过来,其中 /scripts$fastcgi_script_name; 要改成对应的网址路径,例如网址放在 /www/bailitop 就改成

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/bailitop$fastcgi_script_name;
        include        fastcgi_params;
    }


注意不要在 /www/bailitop 后面加 。又或者用变量 $document_root 代替:一般建议用变量来代替:

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

修改完配置,记得reload nginx。

另外一种情况是,当访问的php文件确实是不存在时,访问 /lequ.php, 这个文件并不存在,也会在log中产生上述的错误。因为按照这个规则 "location ~ \.php$ {", 所有 .php 结尾的访问都会传到 fastcgi 中解析,文件不存在导致上述错误,和上面路径写错是一样的道理。期待的结果是当 php 文件不存在时,直接返回 404。 这里可以通过 try_files 来实现,不存在直接返回 404。

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

记得reload nginx。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值