我第一次使用 Nginx,但我对 Apache 和 Linux 非常熟悉。我正在使用现有项目,每当我尝试查看 index.php 时,都会收到 404 文件未找到的消息。
这是 access.log 条目:
2013/06/19 16:23:23 [error] 2216#0: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.ordercloud.lh"
这是站点可用文件:
server {
set $host_path "/home/willem/git/console/www";
access_log /www/logs/console-access.log main;
server_name console.ordercloud;
root $host_path/htdocs;
set $yii_bootstrap "index.php";
charset utf-8;
location / {
index index.html $yii_bootstrap;
try_files $uri $uri/ /$yii_bootstrap?$args;
}
location ~ ^/(protected|framework|themes/\w+/views) {
deny all;
}
#avoid processing of calls to unexisting static files by yii
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(.*)$;
#let yii catch the calls to unexising PHP files
set $fsn /$yii_bootstrap;
if (-f $document_root$fastcgi_script_name){
set $fsn $fastcgi_script_name;
}
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fsn;
#PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fsn;
}
location ~ /\.ht {
deny all;
}
}
我的 /home/willem/git/console 归 www-data:www-data 所有(我的网络用户运行 php 等),出于沮丧我给了它 777 权限...
我最好的猜测是配置有问题,但我无法弄清楚......
更新
所以我把它移到/var/www/
并使用了一个更基本的配置:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name console.ordercloud;
location / {
root /var/www/console/frontend/www/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www;
include fastcgi_params;
}
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
try_files $uri =404;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
}
另外,如果我打电话给localhost/console/frontend/www/index.php
我,我会得到 500 PHP,这意味着它在那里服务。它只是不服务于 console.ordercloud ...
错误消息“未知主脚本”几乎总是
SCRIPT_FILENAME
与nginx 指令中的错误设置有关fastcgi_param
(或不正确的权限,请参阅其他答案)。您在
if
首先发布的配置中使用了 。那么现在应该众所周知,if 是邪恶的并且经常产生问题。在 location 块中设置
root
指令是不好的做法,当然它有效。您可以尝试以下操作:
请注意,以上配置未经测试。您应该
nginx -t
在应用它之前执行以检查 nginx 可以立即检测到的问题。这并不总是
SCRIPT_FILENAME
错误的。也可能是PHP 以错误的用户/组运行。
这个例子是针对Mac OS X的,根据我的经验,这是最麻烦的设置(相比之下 Debian 很容易)——我刚刚从 PHP 5.6 升级到 7.0,使用自制软件和优秀的 josegonzalez 包。
问题是创建了配置文件的新副本。
主配置文件是
/usr/local/etc/php/7.0/php-fpm.conf
,但请注意 末尾的Pool Definitions部分,它包含一个完整的子目录。include=/usr/local/etc/php/7.0/php-fpm.d/*.conf
里面有
php-fpm.d
一个www.conf
文件。默认情况下,它具有:在 OS X 上,您可能需要将其更改为:
(您应该会发现这与
ls -lh
您的 document_root 匹配)不幸的是,如果没有这个改变,你仍然会在你的 Nginx 错误日志中看到这个,即使它在正确的地方寻找文件。
验证它当前运行的是什么:
或更干净:
如何验证脚本文件名是否正确:
(从另一个答案中的 igorsantos07 偷来的)
添加到
http
main 块/usr/local/etc/nginx/nginx.conf
:(第一位需要是你当前使用的任何东西,所以你可以看看它是否正确。)
server
并在您网站的区块中使用您刚刚定义的日志:如果正确,请求 example.com/phpinfo.php 将产生如下内容:
你能简化你现有的配置吗?
您使用的是
location ~ \.php {
从互联网以外的某个地方复制/粘贴的块吗?大多数软件包允许您更快、更干净地完成它。例如在 OS X 上你现在只需要这个:fastcgi_split_path_info、try_files 和 fastcgi_index(默认为 index.php)之类的东西在
/usr/local/etc/nginx/snippets/fastcgi-php.conf
.这又包括
/usr/local/etc/nginx/fastcgi.conf
一个fastcgi_param
设置列表,包括关键的 SCRIPT_FILENAME。永远不要
root
在 PHP 位置块中重复。好的,经过一天的努力,我发现了 3 件事
希望这可以节省一些麻烦!
较新的 nginx (v1.8) 也有同样的问题。较新的版本建议使用
snippets/fastcgi-php.conf;
而不是fastcgi.conf
. 因此,如果您include fastcgi.conf
从教程中复制/粘贴,最终可能会Primary script unknown
在日志中出现错误。“未知主脚本”是由SELinux 安全上下文引起的。
客户端得到响应
nginx error.log 有以下错误信息
所以只需将 Web 根文件夹的安全上下文类型更改为httpd_sys_content_t
nginx/php-fpm 配置有 3 个用户
/etc/nginx/nginx.conf
/etc/nginx/conf.d/www.conf
/etc/php-fpm.d/www.conf
user-1 和 user-2 不必相同。
对于 unix 套接字,user-1 需要与 user-3 相同, 因为 nginx fastcgi_pass 必须对 unix 套接字具有读/写权限。
否则 nginx 会得到502 Bad Gateway,并且 nginx error.log 有以下错误信息
并且 web 根文件夹 (/var/www/show) 的用户/组不必与这 3 个用户中的任何一个相同。
我也有这个问题,我通过交换行
include fastcgi_params
和解决了它fastcgi_param SCRIPT_FILENAME ...
。事实上,nginx 设置了每个 FastCGI 参数的最后一个值,因此您必须将您的值放在 fastcgi_params 中包含的默认值之后。
I solved this problem by closing SELINUX in CentOS7.3 system
steps:
setenforce 0
vim /etc/selinux/config set SELINUX to disabled
检查您的 php-fpm sock 文件的权限,不知何故无法访问:
chmod 755 /usr/local/var/run/php-fpm.sock
然后尝试重启nginx。
尝试在您的 php 位置添加 root 指令。
我发现您的问题是在寻找相同的错误消息,但使用的是 apache + php-fpm(无 nginx)。对我来说,问题是错误位置的斜线:许多设置建议包括以下形式的一行:
通过将最后一个斜杠放在端口号之后,如下所示:
这个问题对我来说消失了。也许你可以做类似的事情