2014/07/02 21:53:02 [error] 4952#0: *8 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstr eam, client: 180.180.180.180, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:
2014/07/02 21:53:02 [error] 4952#0: *8 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstr eam, client: 180.180.180.180, server: localhost, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "100.100.100.100"
nginx+PHP,访问PHP页面显示空白并提示“No input file specified”,nginx错误日志中显示"Primary script unknown" 。这实际上是PHP在display_errors设置为off时,PHP报出的错误。在PHP默认安装下,会将错误输出到stderr或stdout、然后被nginx捕获并记录在error.log中。其根本原因是PHP无法找到nginx告诉它的相应文件,有一些原因可以导致这个问题,如下。
1、给PHP传递了错误的路径
大多数原因是初学者参考网上google的文章来配置导致的。Nginx通过 SCRIPT_FILENAME 这个 fastcgi_param 的值来告诉PHP需要执行的文件。网上大多数的文章将这个参数的值设置为 $document_root$fastcgi_script_name ,有时这样配置还是为了解决"No input file specified" 问题的。但是这会导致路径被“写死”,突然有一天你发现问题出现了。
原因在于nginx有三级配置继承关系:三个层次的区块分别为http、server 和 location,是父子的关系。nginx配置里的命令向下继承,但不会向上继承或交叉。比如你在一个 location中定义了一些东西,是不会应用到其他location中的。
典型的一些初学者,在 location / 中定义 index 和 root ,因为网上的文章是这么教的。当再使用 $document_root 来定义SCRIPT_FILENAME 时,root 并没有真正被定义,因此 SCRIPT_FILENAME 的值仅仅变成了URI,导致PHP找不到该文件。
简单的解决办法是在 server 或 http 区块中定义 root 。
2、不正确的文件权限
当我告诉他你的权限设置不正确时,大多数人都不相信我。他们盯着脚本文件的权限设置,PHP用户有读取权限啊!?很遗憾,这表现出他对Unix用户权限缺乏理解。PHP的启动用户不仅需要脚本文件的读权限,还需要能进入到脚本文件所在的目录(及上层目录),这就需要PHP用户拥有这些目录的执行权限。举一个例子:
说你在 /var/www 目录下有一个 index.php 的脚本文件,需要PHP用户对 /var/www/index.php 有读权限,且需要对 /var 和 /var/www 目录有执行权限。
我就遇到过一次新装的Debian nginx+php环境,在将PHP用户加入到 $document_root 目录拥有者的用户组后,因为没有重启 php-fpm 而导致的权限没有生效问题,查了半天原因。
[...]

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment