nginx访问不存在的php页面,“Primary script unknown”

最近看到nginx的error日志里总有这样的错误:

2015/03/16 17:17:42 [error] 27690#0: *1 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream, client: 96.126.103.244, server: www.hawu.me, request: “GET /heihei.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “www.hawu.me”

原因是按照我先前的nginx配置,nginx总是将.php文件的访问一股脑交给php-fpm,当php-fpm访问不存在的例如这个heihei.php文件时候,就会返回404 file not found页面,并返回错误给nginx。

最好的解决方法,就是在 location ~ \.php$ {} 中添加一句 try_files $uri =404;

    location ~ \.php$ {
        root           /www/hawu;
        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;
    }

表示让nginx在处理.php文件时候,找到的文件就交给php-fpm处理,找不到的文件就直接返回404。

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top